Python 如何使套接字即使在连接关闭后仍继续尝试连接?
因此,我正在编写这个程序,它将允许我使用Python从同一网络上的不同计算机(我自己的ssh版本)运行命令。我希望客户端程序在目标的后台运行,我已经计算出了要执行的后勤。我想做的是启动客户端程序,再也不必启动它,但在我关闭主机上的服务器程序后,我会收到大量错误。我想做的是在我关闭主机程序后,客户端将继续尝试连接到服务器程序,直到我再次运行它。我的客户端程序的代码如下:Python 如何使套接字即使在连接关闭后仍继续尝试连接?,python,sockets,networking,Python,Sockets,Networking,因此,我正在编写这个程序,它将允许我使用Python从同一网络上的不同计算机(我自己的ssh版本)运行命令。我希望客户端程序在目标的后台运行,我已经计算出了要执行的后勤。我想做的是启动客户端程序,再也不必启动它,但在我关闭主机上的服务器程序后,我会收到大量错误。我想做的是在我关闭主机程序后,客户端将继续尝试连接到服务器程序,直到我再次运行它。我的客户端程序的代码如下: import socket import os s = socket.socket(socket.AF_INET, socke
import socket
import os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect():
try:
s.connect(('localhost', 1234))
except:
connect()
while True:
connect()
while True:
try:
msg = s.recv(1024)
os.system(msg.decode("utf-8"))
except:
s.connect('localhost', 1234)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 1234))
s.listen(5)
def main():
while True:
clientsocket, address = s.accept()
while address != None:
message = input("Enter Message: ")
messageb = message.encode("utf-8")
clientsocket.send(messageb)
main()
我的主机程序的代码如下:
import socket
import os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect():
try:
s.connect(('localhost', 1234))
except:
connect()
while True:
connect()
while True:
try:
msg = s.recv(1024)
os.system(msg.decode("utf-8"))
except:
s.connect('localhost', 1234)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 1234))
s.listen(5)
def main():
while True:
clientsocket, address = s.accept()
while address != None:
message = input("Enter Message: ")
messageb = message.encode("utf-8")
clientsocket.send(messageb)
main()
注意:当我将地址放在另一台计算机上时,我将更改localhost的地址。在不了解递归的后果的情况下,切勿执行以下操作来重新启动函数。调用自身的函数称为递归并使用堆栈空间。如果堆栈达到递归限制,这将崩溃
def connect():
try:
s.connect(('localhost', 1234))
except:
connect()
这里有一个解决方案
client.py:
import socket
import time
while True:
s = socket.socket()
try:
print('Trying to connect...')
s.connect(('localhost',8000))
print('Connected.')
try:
while True:
data = s.recv(1024)
if not data: break # server closed connection if nothing received.
print(data)
finally:
s.close()
print('Disconnected.')
except ConnectionError: # Any type of connection error, e.g. refused, aborted, reset.
time.sleep(1)
import socket
import time
s = socket.socket()
s.bind(('',8000))
s.listen()
while True:
c,a = s.accept()
print(f'Connected: {a}')
try:
while True:
c.send(b'message')
time.sleep(1)
except ConnectionError:
c.close()
print(f'Disconnected: {a}')
server.py:
import socket
import time
while True:
s = socket.socket()
try:
print('Trying to connect...')
s.connect(('localhost',8000))
print('Connected.')
try:
while True:
data = s.recv(1024)
if not data: break # server closed connection if nothing received.
print(data)
finally:
s.close()
print('Disconnected.')
except ConnectionError: # Any type of connection error, e.g. refused, aborted, reset.
time.sleep(1)
import socket
import time
s = socket.socket()
s.bind(('',8000))
s.listen()
while True:
c,a = s.accept()
print(f'Connected: {a}')
try:
while True:
c.send(b'message')
time.sleep(1)
except ConnectionError:
c.close()
print(f'Disconnected: {a}')
还要注意,TCP是一种流协议。它没有消息的概念。暂停
时间。退出sleep(1)
,消息将一起运行。一个合适的实现将有一个从字节流中提取完整消息的协议,如固定大小的消息、在消息后传输的大小、分隔符字节(如消息之间的换行符)等。大量错误,如什么?注意:不要在迭代中使用递归。我所说的大量错误是指试图连接时出现错误。