Python Post transfer File script

Source: Internet
Author: User
Tags ssh access

PS: background, can not SSH access to the machine (generally in the middle of the fortress machine Block), but the use of domain names can be accessed in the case of the use of HTTP protocol file upload.

Server-side code (accepts one end of post)

#coding =utf-8from basehttpserver import basehttprequesthandlerimport cgiclass    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(a)         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) #文件大小 (bytes)              print len (Filevalue)              With open (Filename.decode (' utf-8 ') + ' A ', ' WB ')  as f:                 f.write (Filevalue)          return  if __name__== ' __main__ ':     from basehttpserver  import httpserver    sever = httpserver ((' 0.0.0.0 ', 8000), PostHandler)     print  ' Starting server, use <ctrl-c> to stop '     sever.serve_forever ()

Client (send one end of post)

#coding =utf-8import requestsurl = ' http://xxxxxxxxxxxxxxxxxxx:8000 ' path = U '/users/test/student/py/myspider.py ' Print Pathfiles = {' file ': Open (Path, ' RB ')}r = requests.post (URL, files=files)


This article is from "someone who says I am a tech house" blog, please be sure to keep this source http://1992mrwang.blog.51cto.com/3265935/1597407

Python Post transfer File script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.