首页 文章

Python服务器“通常只允许使用每个套接字地址”

提问于
浏览
15

我正在尝试在python中创建一个非常基本的服务器来监听端口,当客户端尝试连接时创建TCP连接,接收数据,发回一些东西,然后再次监听(并无限期地重复该过程) . 这是我到目前为止:

from socket import *

serverName = "localhost"
serverPort = 4444
BUFFER_SIZE = 1024

s = socket(AF_INET, SOCK_STREAM)
s.bind((serverName, serverPort))
s.listen(1)

print "Server is ready to receive data..."

while 1:
        newConnection, client = s.accept()
        msg = newConnection.recv(BUFFER_SIZE)

        print msg

        newConnection.send("hello world")
        newConnection.close()

有时这似乎工作得很好(如果我将浏览器指向“localhost:4444”,服务器会打印出HTTP GET请求,并且网页会打印文本“hello world”) . 但是当我在最后几分钟关闭它后尝试启动服务器时,偶尔会收到以下错误消息:

Traceback (most recent call last):
  File "path\server.py", line 8, in <module>
    s.bind((serverName, serverPort))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

我正在使用Windows 7在python中编程 . 有关如何解决此问题的任何想法?

6 回答

  • 0

    在调用bind()之前启用SO_REUSEADDR套接字选项 . 这允许地址/端口立即重用,而不是在TIME_WAIT状态下停留几分钟,等待迟到的数据包到达 .

    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    
  • 3

    在Windows上,您可以尝试以下步骤:

    1.检查哪个进程使用该端口 .

    # 4444 is your port number
    netstat -ano|findstr 4444
    

    你会得到这样的东西:

    # 19088 is the PID of the process
    TCP    0.0.0.0:4444           *:*                                    19088
    

    2.杀了这个过程

    tskill 19088
    

    祝好运 .

  • 0

    在@JohnKugelman发布的the article中,声明即使启用了 SO_REUSEADDR ,也无法使用套接字连接到同一个远程端:

    SO_REUSADDR允许您使用卡在TIME_WAIT中的端口,但您仍然无法使用该端口 Build 与其连接的最后一个位置的连接 .

    我看到你只是在测试/玩耍 . 但是,要避免此错误,您确实需要确保正确终止连接 . 你也可能搞乱操作系统的tcp时序:http://www.linuxquestions.org/questions/linux-networking-3/decrease-time_wait-558399/

    出于测试目的,如果你只是以循环方式改变 serverPort 也没关系,您怎么看?

  • 0

    关闭套接字很重要(特别是在Windows上) . 否则,在关闭Python之后你必须等待它超时 .

    将:

    try:
        while 1:
            newConnection, client = s.accept()
            msg = newConnection.recv(BUFFER_SIZE)
    
            print msg
    
            newConnection.send("hello world")
            newConnection.close()
    finally:
        s.close()
    

    救命?

  • 9

    如果您尝试重新运行服务器而不停止服务器的最后一刻,它将无法工作 . 如果你想停止当前的瞬间去

    shell-->restart shell.

    如果您已经在不停止服务器的情况下关闭了shell,请转到后台处理器中的 task managerend task python进程 . 这将停止服务器的最后一刻 .

  • 18

    我将端口号更改为不同的端口号并且可以正常工作 .

    if __name__ == '__main__':
        socketio.run(app, debug = True, use_reloader = False, port=1111)
    

相关问题