-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReplayServer.py
53 lines (39 loc) · 1.31 KB
/
ReplayServer.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
import asyncore
import pickle
import socket
class ReplayServer(asyncore.dispatcher):
def __init__(self, address, buffer):
asyncore.dispatcher.__init__(self)
self.buffer = buffer
self.out_socket = self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(address)
self.listen(5)
def handle_accept(self):
pair = self.accept();
if pair:
conn = pair[0]
print 'Incoming connection'
handler = SendHandler(conn, self)
self.replay(handler)
def handle_close(self):
self.close()
def replay(self, handler):
handler.send(self.buffer)
class SendHandler(asyncore.dispatcher_with_send):
def __init__(self, connection, outgoing_server):
asyncore.dispatcher_with_send.__init__(self, connection)
self.outgoing_server = outgoing_server
def handle_close(self):
asyncore.dispatcher_with_send.handle_close(self)
self.outgoing_server.remove_client(self)
def run():
with open("incoming.log") as f:
data = f.read()
replay = ReplayServer(('localhost', 7999), data)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
run()