Python requests upload file tornado receive file

Source: Internet
Author: User

Requests uploading files
ImportRequestsdefimages (): URL='Http://127.0.0.1:8889/upload/image'Files= {'file': Open ('Desktop.png','RB')} multiple_files= [
('file', ('11.png', Open ('11.png','RB'),'Image/png')),
('file', ('Desktop.png', Open ('Desktop.png','RB'),'Image/png'))
] Headers= {'Api-key':'Inhpewfuzza4mddjbtx4awlppi_oxx1ki8', 'user-agent':'mozilla/5.0 (X11; Linux x86_64) applewebkit/537.36 (khtml, like Gecko) chrome/57.0.2987.98 safari/537.36'
} R= Requests.post (URL, files=multiple_files, headers=headers)#Upload Imager = Requests.post (URL, files=files, headers=headers)#Uploading Files Print(R.text)

tornado 接收文件
#!/usr/bin/env python# Copyright facebook#import tornado.httpserverimport tornado.ioloopimport  Tornado.optionsimport tornado.webimport loggingimport sysimport setting

class Uploadimagehandler (basehandler):
defset_default_headers (self):Print "Setting Headers!!!"Self.set_header ("Access-control-allow-origin","*") Self.set_header ("access-control-allow-headers","X-requested-with") Self.set_header ('Access-control-allow-methods','POST, GET, OPTIONS')    defoptions (self):Pass    defGet (self): Self.write ('Please upload a image URL')    defpost (self): result={} result['Flag'] =0 result['Data'] ="'result['msg'] ="'        Try: Is_admin= Self.get_body_argument ('Is_admin', None) meta= self.request.files['file'][0] Suffix= meta['filename'].split ('.') [-1] FullName, arr, filename= Self.get_full_file_name ('Image', suffix) whileos.path.exists (FullName): Logging.info ('file already exists:'+fullname) FullName, arr, filename= Self.get_full_file_name ('Image', suffix) f= Open (FullName,'WB') F.write (meta['Body']) f.close () result['Data'] = setting.openhost+'/'+arr[0]+'/'+arr[1]+'/'+arr[2]+'/'+arr[3]+'/'+filename result['Flag'] = 1exceptException, E:logging.info ('error:upload Image failing,%s'%str (e)) result['Flag'] =0 result['msg'] ='fail in upload image'Self.write (Simplejson.dumps (result))returnSelf.set_header ("Access-control-allow-origin","*") Self.write (simplejson.dumps (Result))


classApplication (tornado.web.Application):def __init__(self): self.session=Memcache. Client ([setting.memcache_host]) handlers= [(R"/upload/image", Uploadimagehandler)Tornado.web.url (R".*", Tornado.web.StaticFileHandler, dict (Path=setting.imgdir), Name='Static_path')] Tornado.web.Application.__init__(self, handlers)if __name__=="__main__": Tornado.options.parse_command_line () application=application () Http_server= Tornado.httpserver.HTTPServer (Application, xheaders=True) Port= 8889ifLen (SYS.ARGV) > 1: Port= Int (sys.argv[1]) Http_server.listen (port) Loop=tornado.ioloop.IOLoop.instance () logging.info ('File Server running on http://127.0.0.1:%d'%port) Loop.start ()

settin.py

#!/usr/bin/env python#Coding=utf8#file=setting.pyImgdir="/imgdata/"  #picture File Save AddressOpenhost ="http://img.520czj.com"  #Access base URL for picture serverServerName ='Server1'  #a naming prefix for files saved in the picture server, which is used to quickly locate the server where the image is located when the reverse proxy is disabled, which is meaningless if you disable a serverMemcache_host ='127.0.0.1:11211'Domanname='http://admin.520czj.com'

Python requests upload file tornado receive file

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.