Python-确定用户是服务器还是客户端
我正在写一个程序,需要两台电脑之间的通信。我知道如何相互连接和发送消息。当用户打开程序时,它应该监听连接。但是,如果用户单击“连接”,程序应该停止侦听连接,转而连接到用户。我应该如何做到这一点?Python-确定用户是服务器还是客户端,python,sockets,Python,Sockets,我正在写一个程序,需要两台电脑之间的通信。我知道如何相互连接和发送消息。当用户打开程序时,它应该监听连接。但是,如果用户单击“连接”,程序应该停止侦听连接,转而连接到用户。我应该如何做到这一点? 我现在就知道了: MAIN @threaded def getOnline(self): # fires when the user opens the program client.connect(PORT) @threaded def connect(self): # whe
我现在就知道了:
MAIN
@threaded
def getOnline(self):
# fires when the user opens the program
client.connect(PORT)
@threaded
def connect(self):
# when the user clicks connect
global IS_MASTER
IS_MASTER = True
print('Master:', IS_MASTER)
if IS_MASTER:
client.closeConnection()
server.connect(CLIENT_IP, PORT)
class Client():
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, port):
print('Listening to connections')
self.s.bind(('', port))
self.s.listen(1)
conn, addr = self.s.accept()
print('Connected by:', addr)
def closeConnection(self):
print('Not listening any longer')
self.s.close()
sys.exit()
客户端@threaded
def getOnline(self):
# fires when the user opens the program
client.connect(PORT)
@threaded
def connect(self):
# when the user clicks connect
global IS_MASTER
IS_MASTER = True
print('Master:', IS_MASTER)
if IS_MASTER:
client.closeConnection()
server.connect(CLIENT_IP, PORT)
class Client():
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, port):
print('Listening to connections')
self.s.bind(('', port))
self.s.listen(1)
conn, addr = self.s.accept()
print('Connected by:', addr)
def closeConnection(self):
print('Not listening any longer')
self.s.close()
sys.exit()
服务器
class Server():
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, host, port):
self.s.connect((host, port))
print('Connected to:', host)
现在,当我单击“连接”时,我得到:“ConnectionAbortedError:[Errno 53]软件导致连接中止”
任何帮助都将不胜感激
编辑完全回溯
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/threading.py", line 639, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/threading.py", line 596, in run
self._target(*self._args, **self._kwargs)
File "/Users/cedricgeerinckx/Dropbox/Redux/OSX/Redux.py", line 28, in wrapped_f
ret = f(*args, **kwargs)
File "/Users/cedricgeerinckx/Dropbox/Redux/OSX/Redux.py", line 204, in getOnline
client.connect(PORT)
File "/Users/cedricgeerinckx/Dropbox/Redux/OSX/Client.py", line 14, in connect
conn, addr = self.s.accept()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 135, in accept
fd, addr = self._accept()
该异常将在启动时运行的
getOnline
方法中抛出。这意味着当在该方法中建立的连接被connect
方法关闭时,会发生异常。您应该在getOnline
中处理异常:
@threaded
def getOnline(self):
# fires when the user opens the program
try:
client.connect(PORT)
except ConnectionAbortedError as e:
print("Connection shut down!")
什么是完整的回溯?添加到问题服务器
bind
,listen
和accept
,客户端connect
。你的客户端
类正在监听一个端口(服务器就是这样做的),而你的服务器
类正在主动连接到一个给定的地址/端口(客户端就是这样做的)。呜呜,谢谢你指出这一点!非常感谢。将更频繁地在我的代码周围添加try块!