1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import http.client import urllib from http.server import HTTPServer,BaseHTTPRequestHandler import json import multiprocessing
def start_server(): data={'result':'this is a test'} host=('localhost',5002)
class Request(BaseHTTPRequestHandler): def do_GET(self): data={'Method:':self.command,'Path:':self.path} print(data) self.send_response(200) self.send_header('Content-type','application/json') self.end_headers() self.wfile.write(json.dumps(data).encode())
def do_POST(self): length=int(self.headers['Content-Length']) post_data=urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8')) # You now have a dictionary of the post data data={'Method:':self.command,'Path:':self.path,'Post Data':post_data} print(data) self.send_response(200) self.send_header('Content-type','application/json') self.end_headers() self.wfile.write(json.dumps(data).encode())
def do_HEAD(self): data={'Method:':self.command,'Path:':self.path,'Header Content-type':self.headers.get('Content-type')} print(data) self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(json.dumps(data).encode())
def do_PUT(self): data={'Method:':self.command,'Path:':self.path,'Header Content-type':self.headers.get('Content-type')} print(data) path=self.path content_length=int(self.headers.get('content-length')) content=self.rfile.read(content_length) with open('put_datas.txt','ab') as outfile: outfile.write(content) self.send_response(200) self.end_headers() self.wfile.write(json.dumps(data).encode())
server=HTTPServer(host,Request) print('Starting server, listen at: %s:%s'%host) server.serve_forever()
def send_get(url,path,data): conn=http.client.HTTPConnection(url) conn.request('GET',path) r1=conn.getresponse() print(r1.status,r1.reason) data1=r1.read() print(data1) conn.close()
def send_post(url,path,data,header): conn=http.client.HTTPConnection(url) conn.request('POST',path,data,header) r1=conn.getresponse() print(r1.status,r1.reason) data1=r1.read() print(data1) conn.close()
def send_head(url,path,data,header): conn=http.client.HTTPConnection(url) conn.request('HEAD',path,data,header) r1=conn.getresponse() print(r1.status,r1.reason) data1=r1.headers print(data1) conn.close()
def send_put(url,path,filedata,header): conn=http.client.HTTPConnection(url) conn.request('PUT',path,filedata,header) r1=conn.getresponse() print(r1.status,r1.reason) data1=r1.read() print(data1) conn.close()
def client_action(): url='localhost:5002' data={'my post data':'I am client, hello world',} datas=urllib.parse.urlencode(data).encode('utf-8') headers={'Content-type':'application/x-www-form-urlencoded','Accept':'text/plain'} print('-----send get test:-----') send_get(url,path='/index',data='None') print('-----send post test:-----') send_post(url,path='/index',data=datas,header=headers) print('-----send head test:-----') send_head(url,path='/index',data=datas,header=headers) print('-----send put test:-----') tfile=open('test.txt',encoding='utf-8',mode='r') filedatas=tfile.read() fileheaders={'Content-type':'text/plain','Accept':'text/plain','content-length':str(len(filedatas))} send_put(url,path='/index',filedata=filedatas,header=fileheaders)
if __name__=='__main__': ps=[] server_p=multiprocessing.Process(target=start_server,args=()) ps.append(server_p) client_p=multiprocessing.Process(target=client_action,args=()) ps.append(client_p) for p in ps: p.start() for p in ps: p.join()
|