Easy HTTP server with Python

This is a method for quickly creating an HTTP server for a little operation check.

CallbackServer.py


#!/usr/bin/env python

import requests
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse

def start(port, callback):
    def handler(*args):
        CallbackServer(callback, *args)
    server = HTTPServer(('', int(port)), handler)
    server.serve_forever()

class CallbackServer(BaseHTTPRequestHandler):
    def __init__(self, callback, *args):
        self.callback = callback
        BaseHTTPRequestHandler.__init__(self, *args)

    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)
        query = parsed_path.query
        self.send_response(200)
        self.end_headers()
        result = self.callback(query)
        message = '\r\n'.join(result)
        self.wfile.write(message)
        return

It defines a class that sets a callback function based on HTTPServer. Please install requests with pip etc.

How to use it is like this.

simple_test.py


#!/usr/bin/env python
# coding:utf-8

import sys
import CallbackServer

def callback_method(query):
    return ['Hello', 'World!', 'with', query]

if __name__ == '__main__':
    port = sys.argv[1]
    CallbackServer.start(port, callback_method)

If you receive only the port number and pass the port number and the method (callback_method) that will be called when there is HTTP access, the HTTP server will go up without permission. callback_method receives a GET query (behind the? In the URL) as an argument and returns the character string returned as Response with return. If you return a list of character strings, it will be broken by CRLF without permission.

Start up

./simple_test.py 12345

After that, if you access http: // localhost: 12345 /? Hoge = 123 from your browser

Hello
World!
with
hoge=123

Is output.

Recommended Posts

Easy HTTP server with Python
HTTP communication with Python
Local server with python
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Easy folder synchronization with Python
Easy Python compilation with NUITKA-Utilities
Simple HTTP Server for python
[Python] Easy parallel processing with Joblib
Write an HTTP / 2 server in Python
Easy email sending with haste python3
Bayesian optimization very easy with Python
Easy data visualization with Python seaborn.
Easy parallel execution with python subprocess
Easy modeling with Blender and Python
FizzBuzz with Python3
Easy keyword extraction with TermExtract for Python
Scraping with Python
[Python] Super easy test with assert statement
[Python] Easy argument type check with dataclass
Python is easy
[In-Database Python Analysis Tutorial with SQL Server 2017]
Scraping with Python
HTTP split download guy made with Python
Python with Go
Twilio with Python
Integrate with Python
Easy introduction of speech recognition with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
[Easy Python] Reading Excel files with openpyxl
with syntax (Python)
Easy web app with Python + Flask + Heroku
Bingo with python
Zundokokiyoshi with python
Easy image processing in Python with Pillow
[Easy Python] Reading Excel files with pandas
Easy web scraping with Python and Ruby
[Python] Easy Reinforcement Learning (DQN) with Keras-RL
Excel with Python
Microcomputer with Python
Cast with python
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
[Python] Easy introduction to machine learning with python (SVM)
Csv output from Google search with [Python]! 【Easy】
ODBC connection to FileMaker 11 Server Advanced with Python 3
Python Web Content made with Lolipop cheap server
Start a simple Python web server with Docker
Send HTTP with Basic authentication header in Python
Create a "Hello World" (HTTP) server with Tornado
Easy Lasso regression analysis with Python (no theory)
Create an API server quickly with Python + Falcon
Make your Python environment "easy" with VS Code
✨ Easy with Python ☆ Estimated elapsed time after death ✨
Serial communication with Python
Tftp server with Docker
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python