我在linux上收到此错误,但是在Windows中就可以了。
这是用于运行nginx,uwsgi和Python3.7的新Linux服务器(cento7)。我在Python中导入“套接字”和“线程”。过去,我曾尝试在现有服务器上删除“线程”,但与OSError相同:[Errno 22]无效的参数。
import threading
from socket import *
try:
HOST = 'localhost'
PORT = 3333 # The default port for modbus-TCP
ADDR = (HOST, PORT)
server = socket(AF_INET, SOCK_STREAM)
server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server.bind(ADDR)
server.listen(5)
print(PORT, 'Port open.')
except OSError:
print('Port open fail.')
def recs():
while True:
clientsock, clientaddress = server.accept()
if clientaddress[0] not in conn_list:
conn_list.append(clientaddress[0])
conn_dt[clientaddress[0]] = clientsock
ip_ModbusDict[clientaddress[0]] = {}
else:
conn_dt[clientaddress[0]] = clientsock
print(datetime.datetime.now(), clientaddress[0], "connection.")
t = threading.Thread(target=tcplink, args=(clientsock, clientaddress))
t.start()
t1 = threading.Thread(target=recs, args=(), name='recs')
t1.start()
3333 Port open.
Port open fail.
Traceback (most recent call last):
File "./app.py", line 15, in <module>
import tcp
File "./tcp.py", line 40, in <module>
clientsock, clientaddress = server.accept()
File "/usr/local/lib/python3.7/socket.py", line 212, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument