simple_http_server
simple_http_server copied to clipboard
Crashes after upload
As soon as I download a file, this server crashes. The file uploads just fine, and the program keeps running, so I can upload/download more files, but when I get a "Connection was reset" error in my browser, it's easy to assume the upload failed.
Here's the terminal output with python3:
server_version: simple_http_server/0.3.1, python_version: Python/3.8.10
sys encoding: utf-8
Serving http on: 0.0.0.0, port: 8000 ... (http://0.0.0.0:8000/)
True File '/dev/shm/simple_http_server/test_file' upload success! by: ('127.0.0.1', 55798)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 55798)
Traceback (most recent call last):
File "/usr/lib/python3.8/socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.8/socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.8/socketserver.py", line 747, in __init__
self.handle()
File "/usr/lib/python3.8/http/server.py", line 427, in handle
self.handle_one_request()
File "/usr/lib/python3.8/http/server.py", line 415, in handle_one_request
method()
File "simple_http_server.py", line 82, in do_POST
f.write(b"<br><a href=\"%s\">back</a>" % self.headers['referer'].encode('utf-8'))
AttributeError: 'NoneType' object has no attribute 'encode'
----------------------------------------
^CYou chose to stop me.
And Here's the python2 output:
server_version: simple_http_server/0.3.1, python_version: Python/2.7.18
sys encoding: utf-8
Serving http on: 0.0.0.0, port: 8000 ... (http://0.0.0.0:8000/)
(True, u"File '/dev/shm/test_file' upload success!", 'by: ', ('127.0.0.1', 55800))
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 55800)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 293, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 655, in __init__
self.handle()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
method()
File "simple_http_server.py", line 82, in do_POST
f.write(b"<br><a href=\"%s\">back</a>" % self.headers['referer'].encode('utf-8'))
File "/usr/lib/python2.7/rfc822.py", line 393, in __getitem__
return self.dict[name.lower()]
KeyError: 'referer'
----------------------------------------
^CYou choose to stop me.
I think I figured it out. My browser is set to never give a referal header (because privacy). So, this server was trying to convert an empty string into binary, which doesn't work.
To work around this issue, I changed line 82 (in simple-http-server.py) to:
f.write((b"<br><a href=\".\">Back</a>").encode('utf-8'))
And that seems to have worked! When you have a hyperlink that points to .
, that's a quick and easy way to reload the current page, without needing any POST data. It's basically a relative path to "wherever you already are". The above code also ignores your referal header, so it's not an issue anymore.
Tested with python versions 2.7.18 and 3.8.10
add PR
I will make a PR. But first, I'd like to point out that my above code doesn't seem to work on Python3. I guess *.encode commands don't work well with string that start with an unquoted b
. Here are two pieces of code that seem to work better:
Option 1:
f.write(b"<br><a href=\".\">Back</a>")
Option 2:
f.write(("<br><a href=\".\">Back</a>").encode('utf-8'))
I have no idea which is better. If someone who knows more about Python than I do could weigh in, that'd be great. Maybe it makes no difference?
Since no one has weighed in, I've decided to make a PR with the shorter one. That seems to work well enough. If you choose to accept the PR, feel free to close this report.