HareCanFly
2015-08-02 12:07
采纳率: 0%
浏览 4.4k

python中socket的端口重用。网上的解决方法不行!

server = socket(AF_INET, SOCK_STREAM)
print 'server is ok!'
server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
print 'setsockopt is ok!'
server.bind(add)
print 'bind is ok!'
server.listen(10)
print 'listen is ok!'

还是会报错。
socket.error: [Errno 98] Address already in use

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • oyljerry 2015-08-03 02:47

    参考这个试试

     import socket
    import SimpleHTTPServer
    import SocketServer
    # import os # uncomment if you want to change directories within the program
    
    PORT = 8000
    
    # Absolutely essential!  This ensures that socket resuse is setup BEFORE
    # it is bound.  Will avoid the TIME_WAIT issue
    
    class MyTCPServer(SocketServer.TCPServer):
        def server_bind(self):
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.socket.bind(self.server_address)
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = MyTCPServer(("", PORT), Handler)
    
    # os.chdir("/My/Webpages/Live/here.html")
    
    httpd.serve_forever()
    
    2 打赏 评论
  • lpfly 2015-08-02 21:59
    打赏 评论

相关推荐 更多相似问题