简体   繁体   中英

Python socket multiple calls using Eventlet

I need to call a socker server multiple times and print its output.

Here is my below code:-

server.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()

client.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'

I use a python eventlet to call the socket_client() 10 times but its not returning the correct result..

You're overriding variable with socket by string received from socket:

s = socket.socket()
...
s = c.recv(50)[::-1]

Pick different variable name for the second case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM