source: trunk/contrib/test_server.py @ 1251

Last change on this file since 1251 was 1014, checked in by douglas, 11 years ago

ADDED Ticket #1217 Simple server to use for testing or demoing Xinha

  • Property svn:executable set to *
File size: 1.8 KB
Line 
1#!/usr/bin/python
2"""Runs a very basic file server so that we can test Xinha.  By default, the
3server runs on port 8080, but you can pass the -p or --port option to change
4the port used."""
5
6import os
7import SimpleHTTPServer
8import SocketServer
9
10# File server for testing Xinha
11
12def __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
54def __usage():
55    """
56    Print the usage information contained in the module docstring
57    """
58    print __doc__
59
60if __name__ == '__main__':
61    __main()
Note: See TracBrowser for help on using the repository browser.