Standalone USB Web Server and Python Interpeter

Discussion in 'Python' started by mcl, Dec 5, 2007.

  1. mcl

    mcl Guest

    I would like to have a USB pen drive, which can execute python scripts
    including CGI versions, which I can take to any Windows PC and run
    without having to install anything on the PC.

    My days of hacking are past so I am looking for something very simple.

    I would envisage a batch file which would ask if the USB web server
    needed to be started or just python scripts and it would return the IP
    address of the server or even some local host name such as 'bobserver'
    which I could give it.

    I need v2.3 python.

    Is this possible

    Thanks

    Richard
     
    mcl, Dec 5, 2007
    #1
    1. Advertising

  2. mcl

    Shane Geiger Guest

    mcl wrote:
    > I would like to have a USB pen drive, which can execute python scripts
    > including CGI versions, which I can take to any Windows PC and run
    > without having to install anything on the PC.
    >
    > My days of hacking are past so I am looking for something very simple.
    >
    > I would envisage a batch file which would ask if the USB web server
    > needed to be started or just python scripts and it would return the IP
    > address of the server or even some local host name such as 'bobserver'
    > which I could give it.
    >
    > I need v2.3 python.
    >
    > Is this possible
    >
    > Thanks
    >
    > Richard
    >



    To the best of my knowledge you should be squared away if you have
    Movable Python and the script below. Let me know whether it works for
    you. (You might have to adjust the path somewhat.)


    Movable Python: http://www.voidspace.org.uk/python/movpy/



    #!/usr/bin/python

    """
    The CGIHTTPServer module
    This is a simple HTTP server that can call external scripts through the
    common gateway interface
    (CGI).
    Example: Using the CGIHTTPServer module
    # File:cgihttpserver-example-1.py

    If you are reading this, you have all the source code necessary to run
    your own Web server.
    Run this with Python.

    Note: This script allows symlinks to places outside the document root.

    """
    try:
    from mywebserver_config import *
    except:
    print "No mywebserver_config module found. Using defaults."
    pass

    import CGIHTTPServer
    import BaseHTTPServer
    class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["/cgi"]


    def writefile(f, data, perms=750): open(f, 'w').write(data) and
    os.chmod(f, perms)
    def readfile(f): return open(f, 'r').read()

    html_template = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>temp</Title>
    </HEAD>
    <BODY>

    <H1 ALIGN="CENTER">Temporary Server</H1>
    This is a temporary server. Do not expect to have access to this
    indefinitely. This is likely running on my laptop.
    </BODY>
    </HTML>
    """


    index2_html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE></Title>
    </HEAD>
    <BODY>

    <H1 ALIGN="CENTER">CGI scripts</H1>
    <a href="cgi/test.py">cgi/test.py</a><br>
    </BODY>
    </HTML>
    """

    example_cgi = """#!/usr/bin/python

    # Required header that tells the browser how to render the text.
    print "Content-Type: text/html\\n\\n"

    # Print a simple message to the display window.
    print "Hello, World!\\n"

    print "<p>What follows are some files in the CGI directory:<p>"
    import commands
    output = commands.getoutput('ls -1 cgi/').splitlines()
    for item in output:
    print "<a href=",
    print item,
    print ">"
    print item,
    print "</a>"
    print "<br>"


    """

    def readip():
    """returns your external IP address by querying dyndns.org
    """
    import re, urllib
    f = urllib.urlopen('http://checkip.dyndns.org')
    s = f.read()
    m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
    return m.group(0)

    print "http://"+readip()+":8000/"

    import urllib
    import os


    originaldir = os.getcwd()

    # For safety, changing the document root
    import tempfile, os
    DOCUMENT_ROOT = tempfile.mkdtemp()
    print "DOCUMENT_ROOT: " + DOCUMENT_ROOT
    os.chdir(DOCUMENT_ROOT)
    writefile(DOCUMENT_ROOT+"/README.html",html_template)

    #print readfile(originaldir+'/'+__file__)
    writefile(DOCUMENT_ROOT+"/"+os.path.basename(os.getcwd()+'/'+__file__),readfile(originaldir
    +'/'+__file__)) # write a copy of this file


    # create a cgi directory
    os.mkdir(DOCUMENT_ROOT+'/cgi')
    writefile(DOCUMENT_ROOT+"/cgi/test.py",example_cgi)
    os.chmod(DOCUMENT_ROOT+"/cgi/test.py",755)
    ### not sure why the previous line doesn't work, but this next
    (os-dependent) line does work (on some OSes):
    os.system('chmod 755 ' + DOCUMENT_ROOT+"/cgi/test.py")



    ### INDEX2.HTML
    writefile(DOCUMENT_ROOT+"/index2.html",index2_html)
    os.system('chmod 755 ' + DOCUMENT_ROOT+"/index2.html")



    try:
    os.remove('/tmp/web') # this path is OS-dependent
    os.symlink(DOCUMENT_ROOT, '/tmp/web')
    print "Created symlink /tmp/web"
    except:
    pass



    import os
    os.symlink( "/Users/shanegeiger/Desktop", DOCUMENT_ROOT+'/Desktop' )
    os.symlink( "cgi/test.py", DOCUMENT_ROOT+'/test.py' )


    PORT = 8000
    #httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
    #httpd = BaseHTTPServer.HTTPServer(("10.37.129.2", PORT), Handler)
    httpd = BaseHTTPServer.HTTPServer(("0.0.0.0", PORT), Handler)

    print "serving at port", PORT
    httpd.serve_forever()


    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (Darwin)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFHVmHKyuP8LzpNs84RAp3gAJ4uJGaVLNwdUvHyhl/giL4MPOPBCgCfeGAh
    d4EE6E5XURQC/VjJECewsqY=
    =wrkN
    -----END PGP SIGNATURE-----
     
    Shane Geiger, Dec 5, 2007
    #2
    1. Advertising

  3. mcl

    Johny Guest

    Johny, Dec 5, 2007
    #3
  4. mcl

    mcl Guest

    On 5 Dec, 08:31, Shane Geiger <> wrote:
    > mcl wrote:
    > > I would like to have a USB pen drive, which can execute python scripts
    > > including CGI versions, which I can take to any Windows PC and run
    > > without having to install anything on the PC.

    >
    > > My days of hacking are past so I am looking for something very simple.

    >
    > > I would envisage a batch file which would ask if the USB web server
    > > needed to be started or just python scripts and it would return the IP
    > > address of the server or even some local host name such as 'bobserver'
    > > which I could give it.

    >
    > > I need v2.3 python.

    >
    > > Is this possible

    >
    > > Thanks

    >
    > > Richard

    >
    > To the best of my knowledge you should be squared away if you have
    > Movable Python and the script below. Let me know whether it works for
    > you. (You might have to adjust the path somewhat.)
    >
    > Movable Python: http://www.voidspace.org.uk/python/movpy/
    >
    > #!/usr/bin/python
    >
    > """
    > The CGIHTTPServer module
    > This is a simple HTTP server that can call external scripts through the
    > common gateway interface
    > (CGI).
    > Example: Using the CGIHTTPServer module
    > # File:cgihttpserver-example-1.py
    >
    > If you are reading this, you have all the source code necessary to run
    > your own Web server.
    > Run this with Python.
    >
    > Note: This script allows symlinks to places outside the document root.
    >
    > """
    > try:
    > from mywebserver_config import *
    > except:
    > print "No mywebserver_config module found. Using defaults."
    > pass
    >
    > import CGIHTTPServer
    > import BaseHTTPServer
    > class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    > cgi_directories = ["/cgi"]
    >
    > def writefile(f, data, perms=750): open(f, 'w').write(data) and
    > os.chmod(f, perms)
    > def readfile(f): return open(f, 'r').read()
    >
    > html_template = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    > Transitional//EN">
    > <HTML>
    > <HEAD>
    > <TITLE>temp</Title>
    > </HEAD>
    > <BODY>
    >
    > <H1 ALIGN="CENTER">Temporary Server</H1>
    > This is a temporary server. Do not expect to have access to this
    > indefinitely. This is likely running on my laptop.
    > </BODY>
    > </HTML>
    > """
    >
    > index2_html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    > Transitional//EN">
    > <HTML>
    > <HEAD>
    > <TITLE></Title>
    > </HEAD>
    > <BODY>
    >
    > <H1 ALIGN="CENTER">CGI scripts</H1>
    > <a href="cgi/test.py">cgi/test.py</a><br>
    > </BODY>
    > </HTML>
    > """
    >
    > example_cgi = """#!/usr/bin/python
    >
    > # Required header that tells the browser how to render the text.
    > print "Content-Type: text/html\\n\\n"
    >
    > # Print a simple message to the display window.
    > print "Hello, World!\\n"
    >
    > print "<p>What follows are some files in the CGI directory:<p>"
    > import commands
    > output = commands.getoutput('ls -1 cgi/').splitlines()
    > for item in output:
    > print "<a href=",
    > print item,
    > print ">"
    > print item,
    > print "</a>"
    > print "<br>"
    >
    > """
    >
    > def readip():
    > """returns your external IP address by querying dyndns.org
    > """
    > import re, urllib
    > f = urllib.urlopen('http://checkip.dyndns.org')
    > s = f.read()
    > m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
    > return m.group(0)
    >
    > print "http://"+readip()+":8000/"
    >
    > import urllib
    > import os
    >
    > originaldir = os.getcwd()
    >
    > # For safety, changing the document root
    > import tempfile, os
    > DOCUMENT_ROOT = tempfile.mkdtemp()
    > print "DOCUMENT_ROOT: " + DOCUMENT_ROOT
    > os.chdir(DOCUMENT_ROOT)
    > writefile(DOCUMENT_ROOT+"/README.html",html_template)
    >
    > #print readfile(originaldir+'/'+__file__)
    > writefile(DOCUMENT_ROOT+"/"+os.path.basename(os.getcwd()+'/'+__file__),readfile(originaldir
    > +'/'+__file__)) # write a copy of this file
    >
    > # create a cgi directory
    > os.mkdir(DOCUMENT_ROOT+'/cgi')
    > writefile(DOCUMENT_ROOT+"/cgi/test.py",example_cgi)
    > os.chmod(DOCUMENT_ROOT+"/cgi/test.py",755)
    > ### not sure why the previous line doesn't work, but this next
    > (os-dependent) line does work (on some OSes):
    > os.system('chmod 755 ' + DOCUMENT_ROOT+"/cgi/test.py")
    >
    > ### INDEX2.HTML
    > writefile(DOCUMENT_ROOT+"/index2.html",index2_html)
    > os.system('chmod 755 ' + DOCUMENT_ROOT+"/index2.html")
    >
    > try:
    > os.remove('/tmp/web') # this path is OS-dependent
    > os.symlink(DOCUMENT_ROOT, '/tmp/web')
    > print "Created symlink /tmp/web"
    > except:
    > pass
    >
    > import os
    > os.symlink( "/Users/shanegeiger/Desktop", DOCUMENT_ROOT+'/Desktop' )
    > os.symlink( "cgi/test.py", DOCUMENT_ROOT+'/test.py' )
    >
    > PORT = 8000
    > #httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
    > #httpd = BaseHTTPServer.HTTPServer(("10.37.129.2", PORT), Handler)
    > httpd = BaseHTTPServer.HTTPServer(("0.0.0.0", PORT), Handler)
    >
    > print "serving at port", PORT
    > httpd.serve_forever()
    >
    > --
    > Shane Geiger
    > IT Director
    > National Council on Economic Education
    > | 402-438-8958 | http://www.ncee.net
    >
    > Leading the Campaign for Economic and Financial Literacy
    >
    > signature.asc
    > 1KDownload


    Shane,

    WOW

    Thank you for the very detailed reply.

    I will try to get it working and let you know how I get on.

    Richard
     
    mcl, Dec 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand K Rayudu

    invoking interpeter in another interpeter

    Anand K Rayudu, Jan 21, 2004, in forum: Python
    Replies:
    0
    Views:
    274
    Anand K Rayudu
    Jan 21, 2004
  2. galapogos
    Replies:
    4
    Views:
    412
    Kenny McCormack
    May 11, 2007
  3. eric

    standalone python web server

    eric, Dec 27, 2007, in forum: Python
    Replies:
    9
    Views:
    643
    Jason Earl
    Dec 27, 2007
  4. LittleAlex
    Replies:
    0
    Views:
    459
    LittleAlex
    Sep 15, 2008
  5. Gelonida
    Replies:
    4
    Views:
    595
    Gelonida
    Feb 1, 2011
Loading...

Share This Page