- Python HTTP(S) Server — Example
- Mar 20, 2016 13:17 · 913 words · 5 minute read Python
- Example of static Python HTTP server
- Python 3.x
- Python 2.x
- Example with SSL support
- Python 3.x
- Python 2.x
- Advanced Python HTTP server
- do_GET
- do_POST
- Twisted As A Simple Web HTTP(S) Server
- Installation
- Usage
- Options
- Commands
- Docker Example
- Simple HTTP Web Server and Client in Python
- Creating simple python scripts to understand HTTP web server and client without framework
- Step 1: Write HTTP server script using BaseHTTPServer module
- Junian Triajianto
- AdvancedHTTPServer 2.2.0
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Классификаторы
- Описание проекта
- License
- Features
- Dependencies
- Code Documentation
- Changes In Version 2.0
- Powered By AdvancedHTTPServer
Python HTTP(S) Server — Example
Mar 20, 2016 13:17 · 913 words · 5 minute read Python
Python has built-in modules for creating HTTP servers, making it easy for developers to create web servers, serve static files, handle requests, and more.
The standard Python library has a built-in module that can be used as minimalistic HTTP/HTTPS web server. It provides support of the protocol and allows you to extend capabilities by subclassing.
Serve static HTML/CSS files to outside world can be very helpful and handy in many real life situations. For example, to show a client HTML pages you’ve created or stub an API by creating a static file.
Example of static Python HTTP server
Yet another purpose that static web server can serve is to create a dummy API by creating json or/and xml files. The structure of resources organized in sub-folders will provide RESTful-like URLs. E.g. /users/all.json.json may contain dummy records of users. This approach even faster then creating, for instance, a Flask application. No database required, works everywhere. To download data from a remote server. Let’s say there are some difficulties with scp command. It is possible to run simple server on the remote machine and download necessary contents via HTTP.
Python 3.x
python3 -m http.server 8000 --bind 127.0.0.1
Python 2.x
python -m SimpleHTTPServer 8000
Python 2.x can only accept port as a parameter Bind address parameter is not available. Python 2.x Docs.
In both cases contents of the current folder will be accessible via http://127.0.0.1:8000
Example with SSL support
To run secure HTTPs server create a following module:
Python 3.x
from http.server import HTTPServer, BaseHTTPRequestHandler import ssl httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler) httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="path/to/key.pem", certfile='path/to/cert.pem', server_side=True) httpd.serve_forever()
Python 2.x
import BaseHTTPServer, SimpleHTTPServer import ssl httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="path/tp/key.pem", certfile='path/to/cert.pem', server_side=True) httpd.serve_forever()
To generate key and cert files with OpenSSL use following command
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
Examples below will assume Python 3.5+ as an interpreter.
Advanced Python HTTP server
Let’s make our web server a little more advanced by handling requests.
do_GET
Consider the following code:
from http.server import HTTPServer, BaseHTTPRequestHandler class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'Hello, world!') httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) httpd.serve_forever()
This is a very trivial HTTP server that responds Hello, world! to the requester. Note, that self.send_response(200) and self.end_headers() are mandatory, otherwise the response wont be considered as valid. We can check that it actually works by sending a request using HTTPie:
$ http http://127.0.0.1:8000 HTTP/1.0 200 OK Date: Sun, 25 Feb 2018 17:26:20 GMT Server: BaseHTTP/0.6 Python/3.6.1 Hello, world!
Note, that self.wfile is a file like object, thus expects a byte-like objects to the write function. Another way of feeding the wfile is by using BytesIO object (see example below).
do_POST
Let’s handle a POST request now. Full example:
from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'Hello, world!') def do_POST(self): content_length = int(self.headers['Content-Length']) body = self.rfile.read(content_length) self.send_response(200) self.end_headers() response = BytesIO() response.write(b'This is POST request. ') response.write(b'Received: ') response.write(body) self.wfile.write(response.getvalue()) httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) httpd.serve_forever()
The request body can be accessed via self.rfile . It is a BufferedReader so read([size]) method should be executed in order to get the contents. Note, that size should be explicitly passed to the function, otherwise the request will hang and never end.
This is why obtaining content_length is necessary. It could be retrieved via self.headers and converted into an integer. An example above just prints back whatever he receives, like follows:
http http://127.0.0.1:8000 key=value HTTP/1.0 200 OK Date: Sun, 25 Feb 2018 17:46:06 GMT Server: BaseHTTP/0.6 Python/3.6.1 This is POST request. Received:
You may consider to parse the JSON if you like.
Twisted As A Simple Web HTTP(S) Server
Another great example of a web server is Twisted. Clearly, it is much faster than one built in Python and provides lots of features out of the box. It supports SSL without a need to write a single line of code. It supports both Python 3.x and 2.x.
Installation
Usage
To run a twisted as a web server to serve current directory:
You will see the output like follows:
(.venv) andrey@work$ ~/Projects/test_app twistd -no web --path=. 2016-10-23T19:05:02+0300 [twisted.scripts._twistd_unix.UnixAppLogger#info] twistd 16.4.1 (/Users/andrey/Projects/anvileight/.venv/bin/python3.5 3.5.1) starting up. 2016-10-23T19:05:02+0300 [twisted.scripts._twistd_unix.UnixAppLogger#info] reactor class: twisted.internet.selectreactor.SelectReactor. 2016-10-23T19:05:02+0300 [-] Site starting on 8080 2016-10-23T19:05:02+0300 [twisted.web.server.Site#info] Starting factory
Options
-n, –nodaemon don’t daemonize, don’t use default umask of 0077
-o, –no_save do not save state on shutdown
–path= is either a specific file or a directory to be set as the root of the web server. Use this if you have a directory full of HTML, cgi, epy, or rpy files or any other files that you want to be
Commands
web A general-purpose web server which can serve from a filesystem or application resource.
If you are looking for HTTPS and SSL support, consider the following options:
–https= Port to listen on for Secure HTTP.
-c, –certificate= SSL certificate to use for HTTPS. [default: server.pem]
-k, –privkey= SSL certificate to use for HTTPS. [default: server.pem]
Docker Example
Here are an example of Dockerfile I use to serve simple html pages to outside world.
FROM python:3.5 VOLUME ["/code"] ADD . /code WORKDIR /code EXPOSE 5000 CMD ["python", "-m", "http.server", "5000"]
It is possible to write custom handlers and extend the basic functionality. Including creating HTTPS server etc. Find official documentation for python 3 http server is here. Python 2 documentation is here
© Copyright 2023 Andrii Zarubin
Simple HTTP Web Server and Client in Python
Creating simple python scripts to understand HTTP web server and client without framework
This time I’d like to show you how to make a simple HTTP server and client in python. It’s a bit different from other tutorials I’ve ever wrote and I’d like to say that I’m also a beginner in python. But, I’ll try to make sure you understand what I wrote because this tutorial is easy.
First, you need to know what HTTP is.
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.
– HTTP definition based on Wikipedia
In simple words, HTTP is a protocol that used for world wide web communication. So, if you’re browsing the web, downloading data, and hosting a website, you’re using HTTP protocol.
There are many web server software that support HTTP protocol such as apache , nginx , and lighttpd . In this moment, I’d like to show you how to make something like that (a simple version obviously) in python language.
Step 1: Write HTTP server script using BaseHTTPServer module
Luckily, python provides us an HTTP server module, it’s called BaseHTTPServer . We use two classes from this module, namely BaseHTTPRequestHandler and HTTPServer . We also need to use os module to access file in your system.
Junian Triajianto
I am not Groot.
AdvancedHTTPServer 2.2.0
A standalone web server built on Python’s BaseHTTPServer.
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: BSD License (BSD)
Автор: Spencer McIntyre
Сопровождающие
Классификаторы
- Development Status
- 5 — Production/Stable
- Console
- Developers
- End Users/Desktop
- Information Technology
- System Administrators
- OSI Approved :: BSD License
- POSIX
- Python :: 2.7
- Python :: 3.3
- Python :: 3.4
- Python :: 3.5
- Python :: 3.6
- Python :: 3.7
- Internet :: WWW/HTTP :: HTTP Servers
- Software Development :: Libraries :: Python Modules
Описание проекта
Standalone web server built on Python’s BaseHTTPServer
License
AdvancedHTTPServer is released under the BSD 3-clause license, for more details see the LICENSE file.
Features
AdvancedHTTPServer builds on top of Python’s included BaseHTTPServer and provides out of the box support for additional commonly needed features such as: — Threaded request handling — Binding to multiple interfaces — SSL and SNI support — Registering handler functions to HTTP resources — A default robots.txt file — Basic authentication — The HTTP verbs GET, HEAD, POST, and OPTIONS — Remote Procedure Call (RPC) over HTTP — WebSockets
Dependencies
AdvancedHTTPServer does not have any additional dependencies outside of the Python standard library.
The following version of Python are currently supported:
- Python 2.7
- Python 3.3
- Python 3.4
- Python 3.5
- Python 3.6
- Python 3.7
Code Documentation
AdvancedHTTPServer uses Sphinx for internal code documentation. This documentation can be generated from source with the command sphinx-build docs/source docs/html . The latest documentation is kindly hosted on ReadTheDocs at advancedhttpserver.readthedocs.io.
Changes In Version 2.0
- The AdvancedHTTPServer module has been renamed advancedhttpserver
- Classes prefixed with AdvancedHTTPServer have been renamed to have the redundant prefix removed
- The hmac_key option is no longer supported
- A single AdvancedHTTPServer instance can now be bound to multiple ports
- The RequestHandler.install_handlers method has been renamed to on_init
- SERIALIZER_DRIVERS was renamed to g_serializer_drivers
- Support for multiple hostnames with SSL using the SNI extension
- Support for persistent HTTP 1.1 TCP connections
Powered By AdvancedHTTPServer