python requests上传文件 tornado 接收文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python requests上传文件 tornado 接收文件相关的知识,希望对你有一定的参考价值。
requests 上传文件
import requests def images(): 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) # 上传图片 r = requests.post(url, files=files, headers=headers) # 上传文件 print(r.text)
tornado 接收文件
tornado 接收文件
#!/usr/bin/env python # Copyright 2009 Facebook # import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import logging import sys import setting
class UploadImageHandler(BaseHandler):
def set_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‘) def options(self): pass def get(self): self.write(‘please upload a image url‘) def post(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) while os.path.exists(fullname): logging.info(‘已经存在文件:‘ + 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‘] = 1 except Exception, e: logging.info(‘Error: upload image failing,%s‘ % str(e)) result[‘flag‘] = 0 result[‘msg‘] = ‘fail in upload image‘ self.write(simplejson.dumps(result)) return self.set_header("Access-Control-Allow-Origin", "*") self.write(simplejson.dumps(result))
class Application(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 = 8889 if len(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.py imgDir = "/imgData/" # 图片文件保存地址 openHost = "http://img.520czj.com" # 图片服务器的访问基URL serverName = ‘server1‘ # 保存在该图片服务器中文件的命名前缀,用于反向代理时快速定位图片所在服务器,如果禁用一台服务器,该字段无实际意义 memcache_host = ‘127.0.0.1:11211‘ domanName = ‘http://admin.520czj.com‘
以上是关于python requests上传文件 tornado 接收文件的主要内容,如果未能解决你的问题,请参考以下文章