annotate scripts/proxy.py @ 585:87de4513bfdd

0.6dev: Cleaning remaining 'frontend' datetime code - at least down to all code that interface with the various `model` classes that still only works with timestamps for input and output. See #85.
author osimons
date Fri, 24 Jul 2009 22:18:30 +0000
parents 2269b705deb9
children
rev   line source
10
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
1 # Based on the proxy module from the Medusa project
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
2 # Used for inspecting the communication between two BEEP peers
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
3
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
4 import asynchat
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
5 import asyncore
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
6 import socket
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
7 import sys
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
8
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
9
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
10 class proxy_server(asyncore.dispatcher):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
11
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
12 def __init__(self, host, port):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
13 asyncore.dispatcher.__init__ (self)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
14 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
15 self.set_reuse_addr()
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
16 self.there = (host, port)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
17 here = ('', port + 1)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
18 self.bind(here)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
19 self.listen(5)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
20
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
21 def handle_accept(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
22 proxy_receiver(self, self.accept())
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
23
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
24
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
25 class proxy_sender(asynchat.async_chat):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
26
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
27 def __init__(self, receiver, address):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
28 asynchat.async_chat.__init__(self)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
29 self.receiver = receiver
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
30 self.set_terminator(None)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
31 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
32 self.buffer = ''
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
33 self.set_terminator('\r\n')
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
34 self.connect(address)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
35 print 'L:', '<wait for incoming connections>'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
36
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
37 def handle_connect(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
38 print 'L:', '<open connection>'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
39
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
40 def collect_incoming_data(self, data):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
41 self.buffer = self.buffer + data
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
42
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
43 def found_terminator(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
44 data = self.buffer
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
45 self.buffer = ''
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
46 for line in data.splitlines():
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
47 print 'L:', '\x1b[35m' + line + '\x1b[0m'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
48 self.receiver.push(data + '\r\n')
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
49
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
50 def handle_close(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
51 self.receiver.close()
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
52 self.close()
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
53
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
54
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
55 class proxy_receiver(asynchat.async_chat):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
56
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
57 channel_counter = 0
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
58
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
59 def __init__(self, server, (conn, addr)):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
60 asynchat.async_chat.__init__(self, conn)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
61 self.set_terminator('\r\n')
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
62 self.server = server
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
63 self.id = self.channel_counter
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
64 self.channel_counter = self.channel_counter + 1
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
65 self.sender = proxy_sender (self, server.there)
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
66 self.sender.id = self.id
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
67 self.buffer = ''
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
68
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
69 def collect_incoming_data (self, data):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
70 self.buffer = self.buffer + data
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
71
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
72 def found_terminator(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
73 data = self.buffer
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
74 self.buffer = ''
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
75 for line in data.splitlines():
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
76 print 'I:', '\x1b[34m' + line + '\x1b[0m'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
77 self.sender.push (data + '\r\n')
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
78
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
79 def handle_connect(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
80 print 'I:', '<open connection>'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
81
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
82 def handle_close(self):
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
83 print 'I:', '<close connection>'
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
84 self.sender.close()
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
85 self.close()
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
86
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
87
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
88 if __name__ == '__main__':
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
89 if len(sys.argv) < 3:
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
90 print 'Usage: %s <server-host> <server-port>' % sys.argv[0]
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
91 else:
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
92 ps = proxy_server(sys.argv[1], int(sys.argv[2]))
2269b705deb9 Improved the BEEP protocol implementation:
cmlenz
parents:
diff changeset
93 asyncore.loop()
Copyright (C) 2012-2017 Edgewall Software