使用SimpleHTTPServer和SocketServer的简单python服务器,如何在重新运行.py文件之前关闭套接字?

时间:2021-02-09 23:56:37

When I run my python server file simplehttpwebsite.py in the linux shell and I do control+c and run it again I get socket.error: [Errno 98] Address already in use.

当我运行我的python服务器文件simplehttpwebsite。在linux shell中的py,我做control+c,然后再运行它,得到套接字。错误:[Errno 98]地址已经在使用。

How do I make sure the socket closes down when I do ctrl+c?

当我按ctrl+c时,如何确保套接字关闭?

simplehttpwebsite.py

simplehttpwebsite.py

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
server = SocketServer.TCPServer(('0.0.0.0', 8080), Handler)

server.serve_forever()

1 个解决方案

#1


13  

Here is how you do it

这是你怎么做的

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
class MyTCPServer(SocketServer.TCPServer):
    allow_reuse_address = True
server = MyTCPServer(('0.0.0.0', 8080), Handler)

server.serve_forever()

IMHO this isn't very well documented, and it should definitely be the default.

IMHO并没有很好的文档化,它应该是默认的。

#1


13  

Here is how you do it

这是你怎么做的

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
class MyTCPServer(SocketServer.TCPServer):
    allow_reuse_address = True
server = MyTCPServer(('0.0.0.0', 8080), Handler)

server.serve_forever()

IMHO this isn't very well documented, and it should definitely be the default.

IMHO并没有很好的文档化,它应该是默认的。