Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

HTTP(s) and SMTP testing server

Project Description

HTTP(s) and SMTP servers which can be run within a Python process. Serving from different thread along with application and tests, exposing a simple thread-safe API, so the calling code can control how the server behaves.

Sometimes integration tests cannot do with mocking the socket.socket function avoiding real networking, this partially solves the problem by providing a real server which is easy to use and can perform real network communication in a controlled and reliable way.


  • Runs in a different thread along with your tests.
  • Control server responses and behaviour.
  • Access to server internal state and data after or during the request.
  • HTTPs support, it bundles a self-signed certificate useful for testing.
  • SMTP support which will collect and parse all your outgoing email.
  • History of all performed requests/responses.

Supports python 2.7 3.2, 3.3 and 3.4


Functions that return a running server instance:

>>> server = start_server()

Or context managers for limited use:

>>> with http_server() as server:
>>> with smtp_server() as server:
...     server.inbox

Mixin classes

Mixins that include an working server as self.server.

import requests
from httptestserver import HttpsTestServer

class TestApplication(HttpsTestServer):

    # Test what was actually get by the server
    def test_it_should_send_headers(self):
        headers = {'key': 'value'}

        requests.get(self.default_url, headers=headers)

        assert['headers']['key'] == 'value'

    # Control server responses
    def test_it_should_parse_json_response(self):['headers'] = {'Content-Type': 'application/json'}['response_content'] = "{'key': 'value'}"

        response = requests.get(self.default_url)

        assert response.json() == {'key': 'value'}

    # Make the server behave as you want
    def test_it_should_raise_timeout_at_2s_wait(self):['response_timeout'] = 2

            requests.get(self.default_url, timeout=1)
        except requests.exceptions.Timeout:
            assert False

    # Access to server's requests/responses history
    def test_it_should_make_two_requests(self):
        requests.get(self.default_url + '2')

        assert len(self.server.history) == 2
        assert self.server.history[-1]['path'] == self.default_url + '2'


In order get a development environment, create a virtualenv and install the desired requirements.

virtualenv env
pip install -r dev-requirements.txt

The included certificate was generated using SSL:

openssl req -new -x509 -keyout server.pem -out server.pem -days 40000 -nodes


To run the tests just use tox or nose:



To generate the documentation change to the docs directory and run make. You need to install the sphinx and changelog packages in order to be able to run the makefile.

cd docs
make html
open build/html/index.html

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(14.0 kB) Copy SHA256 Hash SHA256
Wheel 2.7 Oct 7, 2015
(14.0 kB) Copy SHA256 Hash SHA256
Wheel 3.4 Oct 7, 2015
(11.2 kB) Copy SHA256 Hash SHA256
Source None Oct 7, 2015

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers