python urllib2文件发送问题 [英] python urllib2 file send problem

查看:220
本文介绍了python urllib2文件发送问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过python发布一个文件到服务器,为此我需要将这个文件命名为xmlfile,以便服务器识别输入。

  import urllib2 

url =http:// somedomain
to_send = open('test.xml')。read()
data = {}
data ['xmlfile'] = to_send
f = urllib2.urlopen(url,data)

这不起作用,另外,如何检索响应并保存在某个位置?



换句话说,我想做动作如同Curl一样:

  curl.exe http:// somedomain -F xmlfile=@test.xml -o response .html 


解决方案

我刚读了nimrodm的问题。一个回答提到海报模块。
这个模块可以做 multipart / form-data 编码,所以如果添加另一个依赖项到你的项目不是一个问题,我会用海报模块。 / p>




这不是那么简单。有一个代码片段漂浮在网络上,我用于我的代码,它做的伎俩。

  class RequestWithMethod(urllib2.Request):
def __init __ self,method,* args,** kwargs):
self._method = method
urllib2.Request .__ init __(self,* args,** kwargs)

def get_method (self):
return self._method

类RestRequest(对象):
def __init __(self,base_url):
self.base_url = base_url

def request(self,url,method,headers = {Accept:application / json},data = None,json_response = True):
request = RequestWithMethod(url ='{0 } {1} {2}'。格式(self.base_url,root_url(),url),
method = method,
headers = headers)

if data!无:
data = urllib.urlencode(data)

response = urllib2.urlopen(request,data = data).read()

如果json_response:
return from_json(response)
else:
return response

def GET(self,url,** kwargs):
return self.request ,'GET',** kwargs)

def POST(self,url,** kwargs):
return self.request(url,'POST',** kwargs)

def POST_FILE(self,url,file,headers = {Accept:application / json},data = {},** kwargs):
content_type,body = encode_multipart_formdata ,file)

header ['Content-type'] = content_type
headers ['Content-length'] = str(len(body))

= RequestWithMethod(url ='{0} {1} {2}'。format(self.base_url,root_url(),url),
data = body,
method ='POST',
$ b header = headers)

return from_json(urllib2.urlopen(request).read())

def PUT(self,url,** kwargs):
return self.request(url,'PUT',** kwargs)

def DELETE(self,url,** kwargs):
return self.request(url,'DELETE ',** kwargs)

def encode_multipart_formdata(data,file):
boundary ='---------- ThIs_Is_tHe_bouNdaRY_ $'
L = []
for key,value in data.items():
L.append(' - '+ boundary)
L.append('Content-Disposition:form-data; name ={0}'。format(key))
L.append('')
L.append(value)

key,filename,value = file
L.append(' - '+ boundary)
L.append('Content-Disposition:form-data; name ={0}; filename ={1}。 (key,filename))
content_type = mimetypes.guess_type(filename)[0]或'application / octet-stream'
L.append('Content-Type:{0} ))
L.append('')
L.append(value)

L.append(' - '+ boundary +' - ')
L.append('')
body ='\r\\\
'.join(L)
content_type ='multipart / form-data; border = {0}'。format(boundary)

return content_type,body


I want to post a file to a server via python, for this I need to name this file as "xmlfile" so that server recognizes the input.

import urllib2

url = "http://somedomain"
to_send = open('test.xml').read()
data = {}
data['xmlfile'] = to_send
f = urllib2.urlopen(url, data)

This doesn't work, in addition, how can I retrieve the response and save someplace ?

In other words, I want to do the action as I do with Curl:

curl.exe http://somedomain -F xmlfile=@test.xml -o response.html

解决方案

I just read the question nimrodm referred to. One answer mentions the poster module. This module can do the multipart/form-data encoding, so if adding another dependency to your project is not a problem I would go with the poster module.


This is not as simple as it should be. There is a code snippet floating around the net which I used for my code and it does the trick. You will probably have to adapt it for your needs.

class RequestWithMethod(urllib2.Request):
    def __init__(self, method, *args, **kwargs):
        self._method = method
        urllib2.Request.__init__(self, *args, **kwargs)

    def get_method(self):
        return self._method

class RestRequest(object):
    def __init__(self, base_url):
        self.base_url = base_url

    def request(self, url, method, headers={"Accept" : "application/json"}, data=None, json_response=True):
        request = RequestWithMethod(url='{0}{1}{2}'.format(self.base_url, root_url(), url),
                                    method=method,
                                    headers=headers)

        if data != None:
            data = urllib.urlencode(data)

        response = urllib2.urlopen(request, data=data).read()

        if json_response:
            return from_json(response)
        else:
            return response

    def GET(self, url, **kwargs):
        return self.request(url, 'GET', **kwargs)

    def POST(self, url, **kwargs):
        return self.request(url, 'POST', **kwargs)

    def POST_FILE(self, url, file, headers={"Accept" : "application/json"}, data={}, **kwargs):
        content_type, body = encode_multipart_formdata(data, file)

        headers['Content-type'] = content_type
        headers['Content-length'] = str(len(body))

        request = RequestWithMethod(url='{0}{1}{2}'.format(self.base_url, root_url(), url),
                                    data=body,
                                    method='POST',
                                    headers=headers)

        return from_json(urllib2.urlopen(request).read())

    def PUT(self, url, **kwargs):
        return self.request(url, 'PUT', **kwargs)

    def DELETE(self, url, **kwargs):
        return self.request(url, 'DELETE', **kwargs)

def encode_multipart_formdata(data, file):
    boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
    L = []
    for key, value in data.items():
        L.append('--' + boundary)
        L.append('Content-Disposition: form-data; name="{0}"'.format(key))
        L.append('')
        L.append(value)

    key, filename, value = file
    L.append('--' + boundary)
    L.append('Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(key, filename))
    content_type = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
    L.append('Content-Type: {0}'.format(content_type))
    L.append('')
    L.append(value)

    L.append('--' + boundary + '--')
    L.append('')
    body = '\r\n'.join(L)
    content_type = 'multipart/form-data; boundary={0}'.format(boundary)

    return content_type, body

这篇关于python urllib2文件发送问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆