圆月山庄资源网 Design By www.vgjia.com

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,http,server,post

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com