我正在准备一个演示,我想为其使用一个简单的服务器,而不是nginx或任何其他流行的服务器。
我从python官方页面获得了以下内容:
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
所以,我运行它,一切似乎都很好:
python -m SimpleHTTPServer 8000
如果我转到浏览器,或者卷曲IP地址,它就可以正常工作:
xx.xx.xx.xx - - [31/May/2019 09:36:33] "GET / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:37:43] "GET / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:37:46] "HEAD / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:39:03] "HEAD / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:39:06] "GET / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:39:12] "GET / HTTP/1.1" 200 -
xx.xx.xx.xx - - [31/May/2019 09:39:13] "GET / HTTP/1.1" 200 -
现在,当我通过telnet
连接到服务器并且执行GET
时,服务器将卡住:
$ telnet xx.xx.xx.xx 8000
Trying xx.xx.xx.xx...
Connected to xx.xx.xx.xx.
Escape character is '^]'.
GET / HTTP/1.1
...It just hangs here foever...
当我在服务器上点击ctrl+c
时,它向我显示了发生的异常,我不知道如何在这种情况下解释:
^C----------------------------------------
Exception happened during processing of request from ('xx.xx.xx.xx', 47346)
Traceback (most recent call last):
File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
self.handle()
File "/usr/lib/python3.5/http/server.py", line 422, in handle
self.handle_one_request()
File "/usr/lib/python3.5/http/server.py", line 400, in handle_one_request
if not self.parse_request():
File "/usr/lib/python3.5/http/server.py", line 334, in parse_request
_class=self.MessageClass)
File "/usr/lib/python3.5/http/client.py", line 206, in parse_headers
line = fp.readline(_MAXLINE + 1)
File "/usr/lib/python3.5/socket.py", line 576, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt
----------------------------------------
我还通过SSH将其运行到服务器正在运行的同一台计算机上,并且将请求连接到本地主机。也挂了。
我想知道:
telnet
GET与其他GET有何不同。答案 0 :(得分:2)
- telnet GET与其他GET有何不同。
telnet
在这里没有什么不同。您的服务器正在挂起,因为它正在等待您完成GET请求的键入。为此,您只需要在输入GET请求i后按两次Enter
这是因为:
telnet
您已完成输入
- 在python服务器级别,异常是什么意思,以及如何解决它。
这仅仅是您按下Ctrl + C
的结果,从而中断了运行SimpleHTTPServer
的Python进程。