Skip to main content

A Simple Python HTTP server that echos the request in the response

Project description

A Simple Python HTTP server that echos the request in the response

Provide a simple HTTP server that tries to echo the request back in the response in the most sensible way possible. This can be useful for, testing, debugging, stubbing out a local server in systems that have a hard-coded assumption of making HTTP requests, etc.:

$ python -m httpdecho
Echoing HTTP at http://localhost:8000 ...

Examples

Without specifying a port, the server will try to find the next available port starting at 8000 to try and be as predictable as possible:

>>> import sys
>>> import time
>>> import subprocess
>>> from six.moves import SimpleHTTPServer
>>> startup_delay = 0.5
>>> simple_popen = subprocess.Popen(
...     [sys.executable, '-m', SimpleHTTPServer.__name__]
...     ); time.sleep(1)
>>> echo_popen = subprocess.Popen(
...     [sys.executable, '-m', 'httpdecho']
...     ); time.sleep(1)
>>> echo_popen.poll()
>>> simple_popen.kill()

Once running, HTTP requests are echoed in the responses. The default response body format is basically HTTP header format, from http.client.HTTPMessage:

>>> import io
>>> import requests
>>> import email
>>> get_response = requests.delete('http://localhost:8001')
>>> get_body = email.message_from_string(get_response.text)
>>> print(get_body['Method'])
DELETE
>>> print(get_body['Path'])
/
>>> print(get_body.get_payload())
<BLANKLINE>

Query parameters are also included:

>>> query_response = requests.get(
...     'http://localhost:8001', params=dict(Foo='foo'))
>>> query_body = email.message_from_string(query_response.text)
>>> print(query_body['Foo'])
foo

If the request is a POST or another method that accepts a body on the request, the body or the responses body will contain the POST body:

>>> post_response = requests.patch(
...     'http://localhost:8001', data=dict(Bar='bar'))
>>> post_body = email.message_from_string(post_response.text)
>>> print(post_body.get_payload())
Bar=bar

Shutdown the server:

>>> echo_popen.kill()

TODO

Features for future releases

Content-Type and Accept support for content negotiation:

Return the response body in the format specified in the Accept header if given, otherwise in the same Content-Type as the request.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

httpd-echo-0.1.tar.gz (2.6 kB view details)

Uploaded Source

File details

Details for the file httpd-echo-0.1.tar.gz.

File metadata

  • Download URL: httpd-echo-0.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for httpd-echo-0.1.tar.gz
Algorithm Hash digest
SHA256 c95feb1186d499275f5099df2051454a4e9da8d9df2462c1616272431a7604dd
MD5 b331bcdd59bb5b60aadcd05acda93928
BLAKE2b-256 3e561155fe039c13ab40496cddf469491f8137878edfb0929ee8b71e726283e4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page