Mercurial > bitten > bitten-test
comparison scripts/proxy.py @ 10:2269b705deb9
Improved the BEEP protocol implementation:
* Callbacks for replying to messages.
* Starting of channels implemented.
* Some error handling (though not much yet).
Also, added a sample client that sends a message using the echo protocol.
Finally, added a simple proxy script that outputs the peer-to-peer communication to the console.
author | cmlenz |
---|---|
date | Fri, 10 Jun 2005 20:21:10 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:5d8457aa2025 | 10:2269b705deb9 |
---|---|
1 # Based on the proxy module from the Medusa project | |
2 # Used for inspecting the communication between two BEEP peers | |
3 | |
4 import asynchat | |
5 import asyncore | |
6 import socket | |
7 import sys | |
8 | |
9 | |
10 class proxy_server(asyncore.dispatcher): | |
11 | |
12 def __init__(self, host, port): | |
13 asyncore.dispatcher.__init__ (self) | |
14 self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
15 self.set_reuse_addr() | |
16 self.there = (host, port) | |
17 here = ('', port + 1) | |
18 self.bind(here) | |
19 self.listen(5) | |
20 | |
21 def handle_accept(self): | |
22 proxy_receiver(self, self.accept()) | |
23 | |
24 | |
25 class proxy_sender(asynchat.async_chat): | |
26 | |
27 def __init__(self, receiver, address): | |
28 asynchat.async_chat.__init__(self) | |
29 self.receiver = receiver | |
30 self.set_terminator(None) | |
31 self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
32 self.buffer = '' | |
33 self.set_terminator('\r\n') | |
34 self.connect(address) | |
35 print 'L:', '<wait for incoming connections>' | |
36 | |
37 def handle_connect(self): | |
38 print 'L:', '<open connection>' | |
39 | |
40 def collect_incoming_data(self, data): | |
41 self.buffer = self.buffer + data | |
42 | |
43 def found_terminator(self): | |
44 data = self.buffer | |
45 self.buffer = '' | |
46 for line in data.splitlines(): | |
47 print 'L:', '\x1b[35m' + line + '\x1b[0m' | |
48 self.receiver.push(data + '\r\n') | |
49 | |
50 def handle_close(self): | |
51 self.receiver.close() | |
52 self.close() | |
53 | |
54 | |
55 class proxy_receiver(asynchat.async_chat): | |
56 | |
57 channel_counter = 0 | |
58 | |
59 def __init__(self, server, (conn, addr)): | |
60 asynchat.async_chat.__init__(self, conn) | |
61 self.set_terminator('\r\n') | |
62 self.server = server | |
63 self.id = self.channel_counter | |
64 self.channel_counter = self.channel_counter + 1 | |
65 self.sender = proxy_sender (self, server.there) | |
66 self.sender.id = self.id | |
67 self.buffer = '' | |
68 | |
69 def collect_incoming_data (self, data): | |
70 self.buffer = self.buffer + data | |
71 | |
72 def found_terminator(self): | |
73 data = self.buffer | |
74 self.buffer = '' | |
75 for line in data.splitlines(): | |
76 print 'I:', '\x1b[34m' + line + '\x1b[0m' | |
77 self.sender.push (data + '\r\n') | |
78 | |
79 def handle_connect(self): | |
80 print 'I:', '<open connection>' | |
81 | |
82 def handle_close(self): | |
83 print 'I:', '<close connection>' | |
84 self.sender.close() | |
85 self.close() | |
86 | |
87 | |
88 if __name__ == '__main__': | |
89 if len(sys.argv) < 3: | |
90 print 'Usage: %s <server-host> <server-port>' % sys.argv[0] | |
91 else: | |
92 ps = proxy_server(sys.argv[1], int(sys.argv[2])) | |
93 asyncore.loop() |