我需要多次调用 socker 服务器并打印其输出。
这是我的以下代码:-
服务器.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host, port))
s.listen(5)
print "Server started"
while True:
c, addr = s.accept()
print('Got connection from', addr)
#sprint('Received message == ',c.recv(50))
s = c.recv(50)[::-1]
c.send(s)
c.close()
客户端.py
import socket
from time import sleep
import eventlet
def socket_client():
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host, port))
print "Sending data"
s.sendall("Hello!! How are you")
print(s.recv(1024))
#socket_client()
pile = eventlet.GreenPile()
for x in range(10):
print 'new process started'
pile.spawn(socket_client())
print 'new process started over'
print 'over'
我使用 python eventlet 调用 socket_client() 10 次,但它没有返回正确的结果..
最佳答案
您正在通过从套接字接收到的字符串来使用套接字覆盖变量:
s = socket.socket()
...
s = c.recv(50)[::-1]
为第二种情况选择不同的变量名称。
关于使用Eventlet进行Python套接字多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32106020/