Created
March 13, 2013 16:10
-
-
Save schimfim/5153629 to your computer and use it in GitHub Desktop.
Switched to BaseHTTPServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Run current script as web application. | |
When run from the editor action menu, | |
this script will take the file currently | |
open in the editor and run it as a | |
request handler for an HTTPServer. The | |
file must define a class "Handler" as | |
a subclass of BaseHTTPRequestHandler to | |
handle the requests (see example below). | |
After starting the server, it is | |
accessed using the built-in webbrowser. | |
Installation: Put this script in the | |
editor action menu. | |
For more info, see the documentation for | |
built-in module BaseHTTPServer. | |
As a simple example, copy the following | |
code to a new script and run 'run_srv' | |
from the actions menu. It uses | |
SimpleHTTPRequestHandler, which is a | |
subclass of BaseHTTPRequestHandler. | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
Handler = SimpleHTTPRequestHandler | |
''' | |
from BaseHTTPServer import HTTPServer | |
import webbrowser | |
import editor | |
p = editor.get_text() | |
exec(p) | |
if 'Handler' in locals(): | |
httpd = HTTPServer(("", 8000), Handler) | |
webbrowser.open('http://localhost:8000', stop_when_done = True) | |
httpd.serve_forever() | |
else: | |
print 'No class "Handler" found' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment