Skip to content

Instantly share code, notes, and snippets.

@andystanton
andystanton / Tiny Python 3 HTTP 1.1 Server.md
Last active March 25, 2024 22:43
Tiny Python 3 HTTP/1.1 Server

What?

This is a Python 3 HTTP/1.1 server bound to 127.0.0.1 on port 8000 using only the standard library.

Why?

I wanted a simple Python 3 web server using HTTP 1.1 that can perform simple HTTP operations and depends only on Python and standard library modules. I couldn't find an example so I made one.

The example demonstrates how to accept different methods, parse the url and query strings, read data sent to the server, parse and return json, return responses with different status codes and headers, handle graceful termination, and override the default error logging.

@maurostorch
maurostorch / server.py
Created January 31, 2014 19:33
Simple and light HTTP server in Python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from httplib import HTTPResponse
from os import curdir,sep
#Create a index.html aside the code
#Run: python server.py
#After run, try http://localhost:8080/
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
# generate server.xml with the following command:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
# run as follows:
# python simple-https-server.py
# then in your browser, visit:
# https://localhost:4443
import BaseHTTPServer, SimpleHTTPServer
import ssl