2014-06-03 18:32:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
"""
|
|
|
|
|
<Program>
|
|
|
|
|
simple_server.py
|
|
|
|
|
|
|
|
|
|
<Author>
|
2014-04-22 19:03:42 +00:00
|
|
|
Konstantin Andrianov.
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
<Started>
|
2014-04-22 19:03:42 +00:00
|
|
|
February 15, 2012.
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
<Copyright>
|
|
|
|
|
See LICENSE for licensing information.
|
|
|
|
|
|
|
|
|
|
<Purpose>
|
|
|
|
|
This is a basic server that was designed to be used in conjunction with
|
|
|
|
|
test_download.py to test download.py module.
|
|
|
|
|
|
2014-04-22 19:03:42 +00:00
|
|
|
<Reference>
|
2014-03-31 22:28:54 +00:00
|
|
|
SimpleHTTPServer:
|
|
|
|
|
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
|
|
|
|
|
"""
|
|
|
|
|
|
2014-04-29 18:27:34 +00:00
|
|
|
# Help with Python 3 compatibility, where the print statement is a function, an
|
|
|
|
|
# implicit relative import is invalid, and the '/' operator performs true
|
|
|
|
|
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
import sys
|
|
|
|
|
import random
|
2014-04-22 19:03:42 +00:00
|
|
|
|
2015-06-02 14:28:02 +00:00
|
|
|
import six
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
PORT = 0
|
|
|
|
|
|
|
|
|
|
def _port_gen():
|
|
|
|
|
return random.randint(30000, 45000)
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
try:
|
|
|
|
|
PORT = int(sys.argv[1])
|
|
|
|
|
if PORT < 30000 or PORT > 45000:
|
|
|
|
|
raise ValueError
|
2014-04-22 19:03:42 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
except ValueError:
|
|
|
|
|
PORT = _port_gen()
|
2014-04-22 19:03:42 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
else:
|
|
|
|
|
PORT = _port_gen()
|
|
|
|
|
|
2014-04-22 19:03:42 +00:00
|
|
|
Handler = six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler
|
|
|
|
|
httpd = six.moves.socketserver.TCPServer(('', PORT), Handler)
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
httpd.serve_forever()
|