Python socket listener keep down
Python socket listener keep down
我有 python 个项目。该项目正在监听一个端口。这是我的代码:
import socket
conn = None # socket connection
###
# port listener
# @return nothing
###
def listenPort():
global conn
conn = socket.socket()
conn.bind(("", 5555))
conn.listen(5)
在 运行 我的应用程序之后,我检查了 hercules 端口连接。它有效,但我断开连接并再次连接。完成后 5 次 连接 return 错误。我希望听众必须始终工作。我怎样才能得到我想要的应用程序?提前致谢!
编辑
我将 运行 我的应用程序仅在服务器上运行,我将通过 Uptime root 侦听器进行检查。
错误是正常的:
- 你监听一个队列大小为 5 的套接字
- 你从不接受任何连接
=> 您将 5 个连接请求加入队列,第 6 个导致错误。
您必须接受一个请求,将其从侦听队列中删除并使用它(命令accepter = conn.accept()
派生自相关post)
编辑
这是一个功能齐全的示例:
def listenPort():
global conn
conn = socket.socket()
conn.bind(("", 5555))
conn.listen(5)
while True:
s, addr = conn.accept() # you must accept the connection request
print("Connection from ", addr)
while True: # loop until othe side shuts down or close the connection
data = s.recv(17) # do whatever you want with the data
# print(data)
if not data: # stop if connection is shut down or closed
break
# for example stop listening where reading keyword "QUIT" at begin of a packet
# elif data.decode().upper().startswith("QUIT"):
# s.close()
# conn.close()
# return
s.close() # close server side
我有 python 个项目。该项目正在监听一个端口。这是我的代码:
import socket
conn = None # socket connection
###
# port listener
# @return nothing
###
def listenPort():
global conn
conn = socket.socket()
conn.bind(("", 5555))
conn.listen(5)
在 运行 我的应用程序之后,我检查了 hercules 端口连接。它有效,但我断开连接并再次连接。完成后 5 次 连接 return 错误。我希望听众必须始终工作。我怎样才能得到我想要的应用程序?提前致谢!
编辑
我将 运行 我的应用程序仅在服务器上运行,我将通过 Uptime root 侦听器进行检查。
错误是正常的:
- 你监听一个队列大小为 5 的套接字
- 你从不接受任何连接
=> 您将 5 个连接请求加入队列,第 6 个导致错误。
您必须接受一个请求,将其从侦听队列中删除并使用它(命令accepter = conn.accept()
派生自相关post)
编辑
这是一个功能齐全的示例:
def listenPort():
global conn
conn = socket.socket()
conn.bind(("", 5555))
conn.listen(5)
while True:
s, addr = conn.accept() # you must accept the connection request
print("Connection from ", addr)
while True: # loop until othe side shuts down or close the connection
data = s.recv(17) # do whatever you want with the data
# print(data)
if not data: # stop if connection is shut down or closed
break
# for example stop listening where reading keyword "QUIT" at begin of a packet
# elif data.decode().upper().startswith("QUIT"):
# s.close()
# conn.close()
# return
s.close() # close server side