Post processing of python (NG)

This is an example of Post parameter handling not working on a Python server.

Server program

simple_server.py


import http.server
import socketserver
from http.server import BaseHTTPRequestHandler
import cgi
import sys

class PostHandler(BaseHTTPRequestHandler):
	def do_POST(self):
		sys.stderr.write("*** do_POST *** aaa\n")
		try:
			ff = cgi.FieldStorage(
				self.rfile,
				self.headers,
				environ={
				"REQUEST_METHOD": "POST",
				"CONTENT_TYPE": self.headers["Content-Type"],
				},)
			key = ff.getfirst("key","")
			sys.stderr.write("*** do_Post *** key = %s\n" % key)
		except Exception as ee:
			sys.stderr.write("*** error *** in cgi.FieldStorage ***\n")
			sys.stderr.write(str(ee) + "\n")

if __name__ == '__main__':
	sys.stderr.write("*** start ***\n")
	PORT = 8080

	with socketserver.TCPServer(("", PORT), PostHandler) as httpd:
		print("serving at port", PORT)
		sys.stderr.write("*** start ***\n")
		httpd.serve_forever()

server -Execution

python3 simple_server.py

Access from client

  1. Case where POST parameters can be passed
curl -X POST http://localhost:8080/ -d 'key=t1855'
  1. Case where POST parameters cannot be passed
curl -X POST -H "Content-Type: application/json" -d '{"key":"t1855"}' http://localhost:8080/

Server-side error

*** error *** in cgi.FieldStorage ***
write() argument must be str, not bytes
  1. I cannot pass the next case either.
curl -X POST -H "Content-Type: application/json" [email protected] http://localhost:8080/

in01.json


{"key":"t1855"}

Version used for testing (Ubuntu 19.10)

$ python3 --version
Python 3.7.5

Same result for the next version (Arch Linux)

$ python3 --version
Python 3.8.1

Recommended Posts

Post processing of python (NG)
Various processing of Python
Basics of binarized image processing with Python
Introduction of Python
python image processing
(Java, JavaScript, Python) Comparison of string processing
Python file processing
Grayscale by matrix-Reinventor of Python image processing-
Basics of Python ①
Copy of python
Drawing with Matrix-Reinventor of Python Image Processing-
The story of blackjack A processing (python)
Status of each Python processing system in 2020
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Introduction of Python
View the result of geometry processing in Python
Image processing? The story of starting Python for
Addition of fixed processing when starting python interpreter
Python distributed processing Spartan
[Python] Operation of enumerate
List of python modules
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
Unification of Python environment
Copy of python preferences
Basics of Python scraping basics
[python] behavior of argmax
First Python image processing
Usage of Python locals ()
the zen of Python
Text processing in Python
Installation of Python 3.3 rc1
Queue processing in Python
Image processing with Python
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Python string processing illustration
Basic processing of librosa
[Language processing 100 knocks 2020] Summary of answer examples by Python
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Implementation example of simple LISP processing system (Python version)
[Chapter 5] Introduction to Python with 100 knocks of language processing
Examine the close processing of Python dataset (SQLAlchemy wrapper)
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
Python asynchronous processing ~ Full understanding of async and await ~
Summary of date processing in Python (datetime and dateutil)
[Chapter 4] Introduction to Python with 100 knocks of language processing
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
[Python] Correct usage of map
3. Natural language processing with Python 5-1. Concept of sentiment analysis [AFINN-111]
UTF8 text processing in python
python3 Measure the processing speed.