OLD | NEW |
---|---|
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """This is a simple HTTP server used for testing Chrome. | 6 """This is a simple HTTP server used for testing Chrome. |
7 | 7 |
8 It supports several test URLs, as specified by the handlers in TestPageHandler. | 8 It supports several test URLs, as specified by the handlers in TestPageHandler. |
9 By default, it listens on an ephemeral port and sends the port number back to | 9 By default, it listens on an ephemeral port and sends the port number back to |
10 the originating process over a pipe. The originating process can specify an | 10 the originating process over a pipe. The originating process can specify an |
11 explicit port if necessary. | 11 explicit port if necessary. |
12 It can use https if you specify the flag --https=CERT where CERT is the path | 12 It can use https if you specify the flag --https=CERT where CERT is the path |
13 to a pem file containing the certificate and private key that should be used. | 13 to a pem file containing the certificate and private key that should be used. |
14 """ | 14 """ |
15 | 15 |
16 import base64 | 16 import base64 |
17 import BaseHTTPServer | 17 import BaseHTTPServer |
18 import cgi | 18 import cgi |
19 import optparse | 19 import optparse |
20 import os | 20 import os |
21 import re | 21 import re |
22 import shutil | 22 import shutil |
23 # Needed for 2.4 compatibility. | |
24 import simplejson as json | |
cbentzel
2010/11/17 22:32:50
Just import simplejson - simple enough to search-a
akalin
2010/11/17 23:21:35
It turns out that we run under both 2.4 and 2.6, a
| |
23 import SocketServer | 25 import SocketServer |
24 import sys | 26 import sys |
25 import struct | 27 import struct |
26 import time | 28 import time |
27 import urlparse | 29 import urlparse |
28 import warnings | 30 import warnings |
29 | 31 |
30 # Ignore deprecation warnings, they make our output more cluttered. | 32 # Ignore deprecation warnings, they make our output more cluttered. |
31 warnings.filterwarnings("ignore", category=DeprecationWarning) | 33 warnings.filterwarnings("ignore", category=DeprecationWarning) |
32 | 34 |
(...skipping 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1295 | 1297 |
1296 # Instantiate FTP server class and listen to 127.0.0.1:port | 1298 # Instantiate FTP server class and listen to 127.0.0.1:port |
1297 address = ('127.0.0.1', port) | 1299 address = ('127.0.0.1', port) |
1298 server = pyftpdlib.ftpserver.FTPServer(address, ftp_handler) | 1300 server = pyftpdlib.ftpserver.FTPServer(address, ftp_handler) |
1299 listen_port = server.socket.getsockname()[1] | 1301 listen_port = server.socket.getsockname()[1] |
1300 print 'FTP server started on port %d...' % listen_port | 1302 print 'FTP server started on port %d...' % listen_port |
1301 | 1303 |
1302 # Notify the parent that we've started. (BaseServer subclasses | 1304 # Notify the parent that we've started. (BaseServer subclasses |
1303 # bind their sockets on construction.) | 1305 # bind their sockets on construction.) |
1304 if options.startup_pipe is not None: | 1306 if options.startup_pipe is not None: |
1307 server_data = { | |
1308 'port': listen_port | |
1309 } | |
1310 server_data_json = json.dumps(server_data) | |
1311 print 'sending server_data: %s' % server_data_json | |
1312 server_data_len = len(server_data_json) | |
1305 if sys.platform == 'win32': | 1313 if sys.platform == 'win32': |
1306 fd = msvcrt.open_osfhandle(options.startup_pipe, 0) | 1314 fd = msvcrt.open_osfhandle(options.startup_pipe, 0) |
1307 else: | 1315 else: |
1308 fd = options.startup_pipe | 1316 fd = options.startup_pipe |
1309 startup_pipe = os.fdopen(fd, "w") | 1317 startup_pipe = os.fdopen(fd, "w") |
1310 # Write the listening port as a 2 byte value. This is _not_ using | 1318 # First write the data length as an unsigned 4-byte value. This |
1311 # network byte ordering since the other end of the pipe is on the same | 1319 # is _not_ using network byte ordering since the other end of the |
1312 # machine. | 1320 # pipe is on the same machine. |
1313 startup_pipe.write(struct.pack('@H', listen_port)) | 1321 startup_pipe.write(struct.pack('=L', server_data_len)) |
1322 startup_pipe.write(server_data_json) | |
1314 startup_pipe.close() | 1323 startup_pipe.close() |
1315 | 1324 |
1316 try: | 1325 try: |
1317 server.serve_forever() | 1326 server.serve_forever() |
1318 except KeyboardInterrupt: | 1327 except KeyboardInterrupt: |
1319 print 'shutting down server' | 1328 print 'shutting down server' |
1320 server.stop = True | 1329 server.stop = True |
1321 | 1330 |
1322 if __name__ == '__main__': | 1331 if __name__ == '__main__': |
1323 option_parser = optparse.OptionParser() | 1332 option_parser = optparse.OptionParser() |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1355 'option may appear multiple times, indicating ' | 1364 'option may appear multiple times, indicating ' |
1356 'multiple algorithms should be enabled.'); | 1365 'multiple algorithms should be enabled.'); |
1357 option_parser.add_option('', '--file-root-url', default='/files/', | 1366 option_parser.add_option('', '--file-root-url', default='/files/', |
1358 help='Specify a root URL for files served.') | 1367 help='Specify a root URL for files served.') |
1359 option_parser.add_option('', '--startup-pipe', type='int', | 1368 option_parser.add_option('', '--startup-pipe', type='int', |
1360 dest='startup_pipe', | 1369 dest='startup_pipe', |
1361 help='File handle of pipe to parent process') | 1370 help='File handle of pipe to parent process') |
1362 options, args = option_parser.parse_args() | 1371 options, args = option_parser.parse_args() |
1363 | 1372 |
1364 sys.exit(main(options, args)) | 1373 sys.exit(main(options, args)) |
OLD | NEW |