Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: lib/cros_test_proxy.py

Issue 5632002: Create a transparent proxy for test programs to use, with the (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« image_to_live.sh ('K') | « image_to_live.sh ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import select
8 import socket
9 import SocketServer
10 import threading
11
12 class Filter(object):
13 """Base class for data filters.
14
15 Pass an instance of this to CrosTestProxy
16 """
17
18 def setup(self):
19 """This setup method is called once per connection."""
20 pass
21
22 def InBound(self, data):
23 """This method is called once per packet of incoming data.
24
25 The value returned is what is sent through the proxy. If
26 None is returned, the connection will be closed.
27 """
28 return data
29
30 def OutBound(self, data):
31 """This method is called once per packet of outgoing data.
32
33 The value returned is what is sent through the proxy. If
34 None is returned, the connection will be closed.
35 """
36 return data
37
38
39 class CrosTestProxy(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
40 """A transparent proxy for simulating network errors"""
41
42 class _Handler(SocketServer.BaseRequestHandler):
43 """Proxy connection handler that passes data though a filter"""
44
45 def setup(self):
adlr 2010/12/07 02:26:51 please add a docstring for each method/function
dgarrett 2010/12/07 03:48:18 Done.
46 self.server.filter.setup()
47
48 def handle(self):
49 # Open outgoing socket
50 s_in = self.request
51 s_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52 s_out.connect( (self.server.address_out, self.server.port_out) )
sosa 2010/12/07 03:44:36 additional spaces unnecessary
dgarrett 2010/12/07 04:08:51 Done.
53
54 while True:
55 rlist, wlist, xlist = select.select([s_in, s_out], [], [])
56
57 if s_in in rlist:
58 data = s_in.recv(1024)
59 data = self.server.filter.InBound(data)
60 if not data: break
61 try:
62 s_out.sendall(data)
63 except:
64 break
sosa 2010/12/07 03:44:36 when would get either of these exceptions (here an
dgarrett 2010/12/07 04:08:51 Usually because the connections are closed at the
65
66 if s_out in rlist:
67 data = s_out.recv(1024)
68 data = self.server.filter.OutBound(data)
69 if not data: break
70 try:
71 s_in.sendall(data)
72 except:
73 break
74
75 s_in.close()
76 s_out.close()
77
78 def __init__(self,
sosa 2010/12/07 03:44:36 i try to define this at the top of classes...up to
dgarrett 2010/12/07 04:08:51 Done.
79 filter,
80 port_in=8081,
81 address_out='127.0.0.1', port_out=8080):
82 self.port_in = port_in
83 self.address_out = address_out
84 self.port_out = port_out
85 self.filter = filter
86
87 SocketServer.TCPServer.__init__(self,
88 ('', port_in),
89 self._Handler)
90
91 def serve_forever_in_thread(self):
92 """Call serve_forever in a new thread"""
93 server_thread = threading.Thread(target=self.serve_forever)
94 server_thread.setDaemon(True)
95 server_thread.start()
96
97 return server_thread
98
99
100 def main():
101 s = CrosTestProxy(port_in=2222, port_out=22, filter=Filter())
102 s.serve_forever()
103
sosa 2010/12/07 03:44:36 two lines
dgarrett 2010/12/07 04:08:51 Done.
104 if __name__ == '__main__':
105 main()
OLDNEW
« image_to_live.sh ('K') | « image_to_live.sh ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698