Last active
September 27, 2021 08:01
-
-
Save yangfch3/6fe5ab8c08996110c382907e7e21af3f to your computer and use it in GitHub Desktop.
Python Simple HTTP Server | Python 简易 HTTP 服务器
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 argparse import Namespace | |
import traceback | |
import json | |
import urllib | |
import simp_http_serv_helper as helper | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
class HttpHandler(BaseHTTPRequestHandler): | |
def _response(self, path, args): | |
code = 200 | |
rtv = {'c': 0, 'm': 'ok', 'd': ''} | |
try: | |
if args: | |
args = urllib.parse.parse_qs(args).items() | |
args = dict([(k, v[0]) for k, v in args]) | |
else: | |
args = {} | |
if path == "/": | |
rtv["d"] = "\{\}" | |
elif path == "/test": | |
rtv["d"] = "\{\}" | |
else: | |
code = 404 | |
rtv["c"] = 404 | |
rtv["m"] = path + " is not useable" | |
except Exception as e: | |
rtv["c"] = 1 | |
rtv["m"] = 'server_error: ' + str(e) + "\n" + traceback.format_exc() | |
try: | |
rtv = json.dumps(rtv, ensure_ascii=False) | |
except Exception as e: | |
rtv = { | |
'c': 2, | |
'm': 'data format error: ' + str(e) + "\n" + traceback.format_exc(), | |
'd': '' | |
} | |
rtv = json.dumps(rtv, ensure_ascii=False) | |
self.send_response(code) | |
self.send_header('Content-type', 'text/json; charset=utf-8') | |
self.send_header('Access-Control-Allow-Origin', '*') | |
self.end_headers() | |
self.wfile.write(rtv.encode()) | |
def do_GET(self): | |
path, args = urllib.parse.splitquery(self.path) | |
self._response(path, args) | |
def do_POST(self): | |
args = self.rfile.read(int(self.headers['content-length'])).decode("utf-8") | |
self._response(self.path, args) | |
def start_serv(args): | |
my_ip = "127.0.0.1" | |
if args.local == "0": | |
my_ip = helper.get_host_ip() | |
print(my_ip) | |
print(args.port) | |
httpd = HTTPServer((my_ip, int(args.port)), HttpHandler) | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--port", help="http service port", default="8869") | |
parser.add_argument("-l", "--local", help="is local area", default="0") | |
args = parser.parse_args() | |
start_serv(args) |
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
# -*- coding: utf-8 -*- | |
import os | |
import subprocess | |
import sys | |
import platform | |
import shutil | |
import socket | |
def get_host_ip(): | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.connect(('8.8.8.8', 80)) | |
ip = s.getsockname()[0] | |
finally: | |
s.close() | |
return ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment