1 | #!/usr/bin/python |
---|
2 | """Runs a very basic file server so that we can test Xinha. By default, the |
---|
3 | server runs on port 8080, but you can pass the -p or --port option to change |
---|
4 | the port used.""" |
---|
5 | |
---|
6 | import os |
---|
7 | import SimpleHTTPServer |
---|
8 | import SocketServer |
---|
9 | |
---|
10 | # File server for testing Xinha |
---|
11 | |
---|
12 | def __main(): |
---|
13 | """Use the embed_url.py program from the command-line |
---|
14 | |
---|
15 | The embed_url.py program downloads files and processes links in the case of |
---|
16 | HTML files. See embed_url.py -h for more info. This procedure has the |
---|
17 | sole purpose of reading in and verifying the command-line arguments before |
---|
18 | passing them to the embed_url funtion.""" |
---|
19 | |
---|
20 | from getopt import getopt, GetoptError |
---|
21 | from sys import argv, exit, stderr |
---|
22 | |
---|
23 | try: |
---|
24 | options, arguments = getopt(argv[1:], "p:", ["port="]) |
---|
25 | except GetoptError: |
---|
26 | print "Invalid option" |
---|
27 | __usage() |
---|
28 | exit(2) |
---|
29 | |
---|
30 | PORT = 8080 |
---|
31 | for option, value in options: |
---|
32 | if option in ("-p", "--port"): |
---|
33 | try: |
---|
34 | PORT = int(value) |
---|
35 | except ValueError: |
---|
36 | print "'%s' is not a valid port number" % value |
---|
37 | __usage() |
---|
38 | exit(2) |
---|
39 | |
---|
40 | # SimpleHTTPRequestHandler serves data from the current directory, so if we |
---|
41 | # are running from inside contrib, we have to change our current working |
---|
42 | # directory |
---|
43 | if os.path.split(os.getcwd())[1] == 'contrib': |
---|
44 | os.chdir('..') |
---|
45 | |
---|
46 | Handler = SimpleHTTPServer.SimpleHTTPRequestHandler |
---|
47 | |
---|
48 | httpd = SocketServer.TCPServer(("", PORT), Handler) |
---|
49 | |
---|
50 | print "Serving at port %s" % PORT |
---|
51 | print "Try viewing the example at http://localhost:%s/examples/Newbie.html" % PORT |
---|
52 | httpd.serve_forever() |
---|
53 | |
---|
54 | def __usage(): |
---|
55 | """ |
---|
56 | Print the usage information contained in the module docstring |
---|
57 | """ |
---|
58 | print __doc__ |
---|
59 | |
---|
60 | if __name__ == '__main__': |
---|
61 | __main() |
---|