mcl said:
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.
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
(e-mail address removed) | 402-438-8958 |
http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
signature.asc
1KDownload