码迷,mamicode.com
首页 > 编程语言 > 详细

python requests上传文件 tornado 接收文件

时间:2017-09-12 16:05:57      阅读:538      评论:0      收藏:0      [点我收藏+]

标签:line   tip   gecko   tps   col   os.path   copyright   json   文件   

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 接收文件

#!/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 接收文件

标签:line   tip   gecko   tps   col   os.path   copyright   json   文件   

原文地址:http://www.cnblogs.com/aaron-agu/p/7509734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!