# -*- coding: UTF-8 -*- import time import os import sys import urllib from BaseHTTPServer import (HTTPServer, BaseHTTPRequestHandler) def close_std_fd(): f = open(os.devnull, 'w') sys.stdin = f sys.stdout = f sys.stderr = f def daemon(func): pid = os.fork() if pid > 0: return os.setsid() pid = os.fork() if pid > 0: return os.chdir('/') os.umask(0) close_std_fd() func() class MyHandler(BaseHTTPRequestHandler): def do_response(self): print(self.request) print("request path is %s" % self.path) # print("request from ip is %s" % self.client_address[0]) url_path,url_pargs = urllib.splitquery(self.path) print("request url path is %s" %url_path) print("request pargs is %s" %url_pargs) self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write("<h1>Device Static Content</h1>") return def do_GET(self): self.do_response() def do_POST(self): datas = self.rfile.read(int(self.headers['content-length'])) print("post data is %s" %datas) print("post data type is %s" %type(datas)) self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write("<h1>Device Static Content</h1>") def run_server(): server_address = ("", 99) server = HTTPServer(server_address, MyHandler) sa = server.socket.getsockname() print("sa is below") print(sa) print("Serving on %s using port %s ..." %(sa[0], sa[1])) server.serve_forever() if __name__ == '__main__': if "-d" in sys.argv: daemon(run_server) else: run_server()
client-get
import requests import json val = { "name": "adminw", "passwd": "123456" } res = requests.get("http://127.0.0.1:99/abc",params=val,timeout=2) print res.text
对应的 server
path: request url path is /abc
request pargs is passwd=123456&name=adminw
client-post
import requests import json val = { "name": "adminw", "passwd": "123456" } res = requests.post("http://127.0.0.1:99/abc",data=json.dumps(val),timeout=2) print res.text
对应的server
post data is {"passwd": "123456", "name": "adminw"}
post data type is <type 'str'>