Last active
June 4, 2024 19:50
-
-
Save bsdshell/a16eed857e933289da63d58c324b0130 to your computer and use it in GitHub Desktop.
python http server, http json, python http json, python http post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from http.server import BaseHTTPRequestHandler, HTTPServer | |
import socketserver | |
import json | |
import cgi | |
import sqlite3 | |
''' | |
DATE: Mon Jun 3 19:51:44 2024 | |
# How to run it | |
python3 httpserver-sqlite3.py 8000 | |
# GET | |
curl http://localhost:8000 | |
# POST | |
curl -X POST http://localhost:8000 -H 'Content-Type: application/json' -d '{"key1" : "From client"}' | |
Get json from client | |
{'key1' : 'Get JSON'} | |
Reply json to client | |
{'key1' : 'Reply from Server'} | |
''' | |
class Server(BaseHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'application/json') | |
self.end_headers() | |
def do_HEAD(self): | |
self._set_headers() | |
# GET sends back a Hello world message | |
def do_GET(self): | |
self._set_headers() | |
response = json.dumps({'hello': 'world', 'received': 'ok'}) | |
byteResp = bytes(response, 'utf-8') | |
self.wfile.write(byteResp) | |
# POST echoes the message adding a JSON field | |
def do_POST(self): | |
ctype, pdict = cgi.parse_header(self.headers.get('content-type')) | |
# refuse to receive non-json content | |
if ctype != 'application/json': | |
self.send_response(400) | |
self.end_headers() | |
return | |
# read the message and convert it into a python dictionary | |
length = int(self.headers.get('content-length')) | |
payload_str = self.rfile.read(length).decode('utf-8') | |
jsonItem = json.loads(payload_str) | |
print('jsonItem=>', jsonItem) | |
mydb = '/Users/aaa/myfile/bitbucket/database/test_sqlite3.db' | |
con = sqlite3.connect(mydb) | |
cur = con.cursor() | |
cur.execute('select * from todoApp') | |
users = cur.fetchall() | |
''' | |
for row in users: | |
print(row) | |
''' | |
print(jsonItem) | |
cur.execute(''' | |
INSERT INTO todoApp (key_item, todo_item) VALUES(?,?) | |
''', (jsonItem['key'], jsonItem['item'])) | |
print("cmd:", jsonItem['cmd']) | |
print("key:", jsonItem['key']) | |
print("item:", jsonItem['item']) | |
con.commit(); | |
# send the message back | |
self._set_headers() | |
self.wfile.write(bytes(json.dumps(jsonItem), 'utf-8')) | |
def run(server_class=HTTPServer, handler_class=Server, port=8008): | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
print('Starting httpd on port %d...' % port) | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
from sys import argv | |
if len(argv) == 2: | |
run(port=int(argv[1])) | |
else: | |
run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment