Skip to content

Instantly share code, notes, and snippets.

@schimfim
Created March 13, 2013 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schimfim/5153629 to your computer and use it in GitHub Desktop.
Save schimfim/5153629 to your computer and use it in GitHub Desktop.
Switched to BaseHTTPServer
'''
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