<i id='R9KHh'><tr id='R9KHh'><dt id='R9KHh'><q id='R9KHh'><span id='R9KHh'><b id='R9KHh'><form id='R9KHh'><ins id='R9KHh'></ins><ul id='R9KHh'></ul><sub id='R9KHh'></sub></form><legend id='R9KHh'></legend><bdo id='R9KHh'><pre id='R9KHh'><center id='R9KHh'></center></pre></bdo></b><th id='R9KHh'></th></span></q></dt></tr></i><div id='R9KHh'><tfoot id='R9KHh'></tfoot><dl id='R9KHh'><fieldset id='R9KHh'></fieldset></dl></div>

<tfoot id='R9KHh'></tfoot>
    • <bdo id='R9KHh'></bdo><ul id='R9KHh'></ul>

    <small id='R9KHh'></small><noframes id='R9KHh'>

    <legend id='R9KHh'><style id='R9KHh'><dir id='R9KHh'><q id='R9KHh'></q></dir></style></legend>

      1. Python服务器“通常只允许每个套接字地址使用一次"

        时间:2023-10-18

            <bdo id='h2zil'></bdo><ul id='h2zil'></ul>
            <legend id='h2zil'><style id='h2zil'><dir id='h2zil'><q id='h2zil'></q></dir></style></legend>

              1. <small id='h2zil'></small><noframes id='h2zil'>

              2. <tfoot id='h2zil'></tfoot>
                  <tbody id='h2zil'></tbody>
                <i id='h2zil'><tr id='h2zil'><dt id='h2zil'><q id='h2zil'><span id='h2zil'><b id='h2zil'><form id='h2zil'><ins id='h2zil'></ins><ul id='h2zil'></ul><sub id='h2zil'></sub></form><legend id='h2zil'></legend><bdo id='h2zil'><pre id='h2zil'><center id='h2zil'></center></pre></bdo></b><th id='h2zil'></th></span></q></dt></tr></i><div id='h2zil'><tfoot id='h2zil'></tfoot><dl id='h2zil'><fieldset id='h2zil'></fieldset></dl></div>
                  本文介绍了Python服务器“通常只允许每个套接字地址使用一次"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 python 中创建一个非常基本的服务器,它侦听端口,在客户端尝试连接时创建 TCP 连接,接收数据,发回某些内容,然后再次侦听(并无限期地重复该过程).这是我目前所拥有的:

                  I'm trying to create a very basic server in python that listens in on a port, creates a TCP connection when a client tries to connect, receives data, sends something back, then listens again (and repeats the process indefinitely). This is what I have so far:

                  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").但是当我在最后几分钟关闭服务器后尝试启动服务器时,偶尔会收到以下错误消息:

                  Sometimes this seems to work perfectly well (if I point my browser to "localhost:4444" the server prints out the HTTP GET request and the webpage print the text "hello world"). But I'm getting the following error message sporadically when I try to start the server after closing it in the last few minutes:

                  Traceback (most recent call last):
                    File "pathserver.py", line 8, in <module>
                      s.bind((serverName, serverPort))
                    File "C:Python27libsocket.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 中编程.关于如何解决这个问题的任何想法?

                  I'm programming in python using Windows 7. Any ideas on how to fix this?

                  推荐答案

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

                  Enable the SO_REUSEADDR socket option before calling bind(). This allows the address/port to be reused immediately instead of it being stuck in the TIME_WAIT state for several minutes, waiting for late packets to arrive.

                  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                  

                  这篇关于Python服务器“通常只允许每个套接字地址使用一次"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:matplotlib 标题中的乳胶和文本 下一篇:Python [Errno 98] 地址已在使用中

                  相关文章

                • <legend id='qgvh9'><style id='qgvh9'><dir id='qgvh9'><q id='qgvh9'></q></dir></style></legend>

                    <i id='qgvh9'><tr id='qgvh9'><dt id='qgvh9'><q id='qgvh9'><span id='qgvh9'><b id='qgvh9'><form id='qgvh9'><ins id='qgvh9'></ins><ul id='qgvh9'></ul><sub id='qgvh9'></sub></form><legend id='qgvh9'></legend><bdo id='qgvh9'><pre id='qgvh9'><center id='qgvh9'></center></pre></bdo></b><th id='qgvh9'></th></span></q></dt></tr></i><div id='qgvh9'><tfoot id='qgvh9'></tfoot><dl id='qgvh9'><fieldset id='qgvh9'></fieldset></dl></div>

                    <tfoot id='qgvh9'></tfoot>

                    <small id='qgvh9'></small><noframes id='qgvh9'>

                      • <bdo id='qgvh9'></bdo><ul id='qgvh9'></ul>