A simple mock server that simply embeds the HTTP request header in the body of the response and returns it.

I wanted a little mock server that just embeds the HTTP request header in the response and returns it when checking the operation of proxy_pass of nginx.

Speaking of simple HTTP servers, python's one-liner is enough for just static file distribution,

$ sudo python -m SimpleHTTPServer 80

This time I wanted to embed the HTTP request header in the body of the response and return it, so I wrote a simple python script. By the way, python is implemented in python because it is included in many Linux distributions by default and can be done with only the standard library and no additional dependent modules. By the way, python is 2.7 series.

$ python --version
Python 2.7.12

Create a script like this with the name mock_server.py.

mock_server.py


#!/usr/bin/env python

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write('%s %s %s\n' % (self.command, self.path, self.request_version))
        self.wfile.write(self.headers)
        self.finish()
        self.connection.close()

port = 80
server = HTTPServer(('', port), RequestHandler)
server.serve_forever()

Execute via sudo for the convenience of listening on port 80.

$ sudo python mock_server.py

Try curl.

$ curl -H "Host: hoge.example.com" -H "X-MOCK-PROXY-PASS: hoge.example.com" http://127.0.0.1/hoge
GET /hoge HTTP/1.1
Host: hoge.example.com
User-Agent: curl/7.47.1
Accept: */*
X-MOCK-PROXY-PASS: hoge.example.com

If you put nginx you want to test in between and configure it like curl => nginx => mock server, you can see if the expected HTTP header is passed, and if you fill in the appropriate header for debugging, which location I feel like I can easily test if proxy_pass is done with.

Recommended Posts

A simple mock server that simply embeds the HTTP request header in the body of the response and returns it.
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
Launch a simple WEB server that can check the header
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
Regularly monitor the HTTP response of the web server
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
A simple mock server that simply embeds the HTTP request header in the body of the response and returns it.
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
The background of the characters in the text image is overexposed to make it easier to read.
A script that keeps looking up until the URL is bookmarked with Hatena Bookmark
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
[Python3] Take a screenshot of a web page on the server and crop it further
The result of making a map album of Italy honeymoon in Python and sharing it
A simple Python HTTP server that supports Range Requests
# Function that returns the character code of a string
A script that pings the registered server and sends an email with Gmail a certain number of times when it fails
How to start a simple WEB server that can execute cgi of php and python
An example of a mechanism that returns a prediction by HTTP from the result of machine learning
Django returns the contents of the file as an HTTP response
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Scraping the schedule of Hinatazaka46 and reflecting it in Google Calendar
A formula that simply calculates the age from the date of birth
A function that measures the processing time of a method in python
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer