Python开发技术自从诞生以来就备受人们的关注与喜爱,所以如今参加Python培训学习Python开发技术的小伙伴越来越多,本篇文章小编就和大家分享一篇Python搭建简单的http server,可直接post文件的实例,希望对小伙伴们有所帮助。
Python培训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在线学习小编给大家分享的Python搭建简单的http server
可直接post文件的实例,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。
想要学好Python开发小编给大家推荐口碑良好的扣丁学堂,扣丁学堂有专业老师制定的Python学习路线图辅助学员学习,此外还有与时俱进的Python课程体系和Python视频直播课供大家学习,想要学好Python开发技术的小伙伴快快行动吧。
网友评论