我想使连接自动化。运行代码时面临以下问题。尝试添加断开连接,但是没有用。系统正在连接并且数据正在传输。一旦连接断开,当系统尝试重新连接时,我将面临问题。
import socket,time
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ip = '192.168.xx.x'
port = 4xxx
address = (ip,port)
def connect():
try:
while 1:
print("connecting")
try:
client.connect(address)
print("connected")
except TimeoutError:
print("wait for few mins")
time.sleep(20)
connect()
break
except ConnectionAbortedError:
print("connection aborted wait for few sec")
time.sleep(10)
connect()
connect()
try:
while 1:
datasent = client.send(b'\x01\x04')
if datasent:
print("sent")
data= client.recv(1024)
print(data)
time.sleep(5)
if not datasent:
connect()
except ConnectionResetError:
client.shutdown(socket.SHUT_RDWR)
client.close()
print("wait for 10 sec")
time.sleep(10)
connect()
>Error: Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Data\ckeck\client.py", line 28, in <module>
datasent = client.send(b'\x01\x04\')
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Data\ckeck\client.py", line 41, in <module>
connect()
File "C:\Users\User\eclipse-workspace\Data\ckeck\client.py", line 13, in connect
client.connect(address)
OSError: [WinError 10038] An operation was attempted on something that is not a socket