Skip to content

Instantly share code, notes, and snippets.

@seralf
Last active July 17, 2023 02:49
Show Gist options
  • Save seralf/6193246 to your computer and use it in GitHub Desktop.
Save seralf/6193246 to your computer and use it in GitHub Desktop.
Start a local webserver into the current directory, for serving local static files. (This is useful when testing ajax locally on json, for example.)
#!/usr/bin/python
# start an http webserver serving static files from the local directory, using python
python -m SimpleHTTPServer 7777 &
#!/usr/bin/python
# start an http webserver serving static files from the local directory, using python 3+
python -m http.server 7777 &
#!/usr/bin/python
# python-programmed example, as a base for customization:
# this example is not mine, but taken from: http://www.acmesystems.it/python_httpserver
# (where there are other good examples! ;-)
#
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 7777
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
self.wfile.write("Hello World !")
return
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment