python 搭建简单的http server,可直接post文件的实例
程序员文章站
2022-05-04 13:45:44
server:
#coding=utf-8
from basehttpserver import basehttprequesthandler
import...
server:
#coding=utf-8 from basehttpserver import basehttprequesthandler import cgi class posthandler(basehttprequesthandler): def do_post(self): form = cgi.fieldstorage( fp=self.rfile, headers=self.headers, environ={'request_method':'post', 'content_type':self.headers['content-type'], } ) self.send_response(200) self.end_headers() self.wfile.write('client: %sn ' % str(self.client_address) ) self.wfile.write('user-agent: %sn' % str(self.headers['user-agent'])) self.wfile.write('path: %sn'%self.path) self.wfile.write('form data:n') for field in form.keys(): field_item = form[field] filename = field_item.filename filevalue = field_item.value filesize = len(filevalue)#文件大小(字节) #print len(filevalue) #print (filename) with open(filename.decode('utf-8'),'wb') as f: f.write(filevalue) return def startserver(): from basehttpserver import httpserver sever = httpserver(("",8080),posthandler) sever.serve_forever() if __name__=='__main__': startserver()
client:
#coding=utf-8 import requests url = "http://172.16.1.101:8080" path = "/home/ly/ly.exe" print path files = {'file': open(path, 'rb')} r = requests.post(url, files=files) print (r.url) print (r.text)
以上这篇python 搭建简单的http server,可直接post文件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Python全栈学习_day010作业