-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon_blocking_server.py
68 lines (57 loc) · 2.22 KB
/
non_blocking_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import avro.datafile
import avro.io
import io
import socket
import struct
from core import process_url
# https://gist.github.com/meatcar/081f9c852928934a7029
# http://nbviewer.jupyter.org/github/hammerlab/bdgenomics-notebook/blob/master/Big-Data-Genomics-Tutorial.ipynb
# PROTOCOL = protocol.parse(open("avro_schemas/page.avpr").read())
class ClientDisconnect(Exception):
pass
def read_block(connection, message_size):
bytes_read = 0
block = b''
print('Receiving message of size: {}'.format(message_size))
while bytes_read < message_size:
data = connection.recv(message_size - bytes_read)
if len(data) == 0:
raise ClientDisconnect()
block += data
bytes_read += len(data)
print("Read {} bytes".format(len(data)))
print("Read {} byte block".format(len(block)))
print('Here is the block I have read {}'.format(block))
return block
def handle_client(connection, address):
try:
while True:
size_block = read_block(connection, 4)
message_size, = struct.unpack("!L", size_block)
message_block = read_block(connection, message_size)
message_buf = io.BytesIO(message_block)
view = message_buf.getbuffer()
print('////////////////')
print(message_buf.getvalue())
print(view)
# reader = avro.datafile.DataFileReader(message_buf, avro.io.DatumReader())
# for thing in reader:
# page = process_url(thing['url'])
# print('New es page generated with id:\t', page._id)
# reader.close()
except ClientDisconnect as e:
print("Client Disconnected")
def start_server(host='127.0.0.1', port=12345):
""" http://layer0.authentise.com/getting-started-with-avro-and-python-3.html"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(10)
print('serving at {}:{}'.format(host, port))
while True:
print('waiting on client to connect')
conn, addr = sock.accept()
handle_client(conn, addr)
conn.close()
if __name__ == '__main__':
start_socket_server()