Created
August 14, 2014 23:13
-
-
Save lkraider/abd24f224751a6edc5c5 to your computer and use it in GitHub Desktop.
Example of using gevent for async socket server and client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent | |
import gevent.server | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
import socket | |
import string | |
import random | |
import filecmp | |
import os | |
""" | |
Example output: | |
$ python async-socket.py | |
new connection by ('127.0.0.1', 58020) | |
client got data! | |
client got data! | |
client data matches server | |
...forever | |
""" | |
HOST = '' | |
PORT = 42000 | |
def server_handler(connection, address): | |
print 'new connection by', address | |
chunk_size = 1024 * 1024 # how many bytes to send | |
data_stream = data_stream_gen() | |
while True: | |
chunk = str.join('', (next(data_stream) for i in xrange(chunk_size))) | |
connection.sendall(chunk) | |
with open('server.txt', 'w') as log: | |
log.write(chunk) | |
def data_stream_gen(): | |
while True: | |
yield random.choice(string.ascii_lowercase) | |
def client(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
chunk_size = 512 * 1024 # how many bytes we want to process each loop | |
while True: | |
data = '' | |
while len(data) < chunk_size: | |
data += s.recv(chunk_size - len(data)) | |
if not data: | |
break | |
# simulate heavy processing | |
print 'client got data!' | |
gevent.sleep(5) | |
with open('client.txt', 'a') as log: | |
log.write(data) | |
compare_data() | |
s.close() | |
def compare_data(): | |
if os.stat('client.txt').st_size >= 1024 * 1024: | |
if filecmp.cmp('client.txt', 'server.txt', shallow=False): | |
print 'client data matches server' | |
else: | |
print 'error between client and server data' | |
with open('client.txt', 'w') as out: | |
out.truncate() | |
if __name__ == '__main__': | |
# clear logs | |
with open('client.txt', 'w') as out: | |
out.truncate() | |
with open('server.txt', 'w') as out: | |
out.truncate() | |
# start server | |
server = gevent.server.StreamServer((HOST, PORT), server_handler) | |
server.start() | |
# start client and wait | |
c = gevent.spawn(client).join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment