uploading file using urllib2

折月煮酒 提交于 2019-12-25 04:45:00

问题


I'm new to python and I'm writing a code to upload a file using urllib2 but I can't make it work.

Here's the code:

class Get(object):
    handlers = list()
    def __init__(self,url):
        self.url = url
        self.request = urllib2.Request(url)
        self.request.add_header('User-Agent',"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13")

    def auth(self,username,password):
        pass_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        pass_mgr.add_password(None, self.url, username, password)
        handler = urllib2.HTTPBasicAuthHandler(pass_mgr)
        self._add_handler(handler)

    def perform(self):
        try:
            opener = self._opener()
            res = opener.open(self.request)
            to_return = {
                'code': res.code,
                'contents': res.read(),
                'url': res.geturl(),
                'headers': dict(res.info())
            }
        except urllib2.URLError as e:
            if hasattr(e, 'reason'):
                print 'Error accessing the server.'
                print 'Reason: ', e.reason
            elif hasattr(e, 'code'):
                print 'The server couldn\'t fulfill the request.'
                print e
        else:
            return to_return

    def _add_handler(self,handler):
        self.handlers.append(handler)

    def _opener(self):
        return urllib2.build_opener(*self.handlers)

class Multipart(object):
    def __init__(self,url):
        super(Multipart,self).__init__(url)
        self.data = list()

    def perform(self):
        b = choose_boundary()

        tmp = "".join(map(lambda x: "--%s \r\n %s" % (b,x), self.data )) 
        tmp += "--%s--\r\n" % b
        self.request.add_data(tmp)
        content_type = 'multipart/form-data; boundary=%s' % b
        self.request.add_unredirected_header('Content-Type', content_type)
        #self.request.add_header("Content-Type","multipart/form-data, boundary=%s" % b)
        return super(HTTP.Multipart,self).perform()

    def set_data(self,data,file = None):
        for i in data:
            if file:
                self.data.append(self._encode_file(i,**data[i]))
            else:
                self.data.append(self._encode_text(i,data[i]))

    def _encode_text(self,key,value):
        return "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n" % (key, value)

    def _encode_file(self,key,path,filename=None,mime_type=None):
        if not exists(path):
            raise RuntimeError('%s not found' % path)

        fname = filename or basename(path)
        mime = mime_type or guess_type(path)[0] or 'application/octet-stream'
        size = getsize(path)
        content = ""
        with open(path,'rb') as fobj:
            content = fobj.read(size)

        converted_text = "Content-Disposition: form-data; name=\"%s\";     filename=\"%s\"\r\n" % (key,fname)
        converted_text += "Content-Transfer-Encoding: binary\r\n"
        converted_text += "Content-Type: %s \r\n" % mime
        converted_text += "Content-Length: %s \r\n" % size
        converted_text += "\r\n %s \r\n" % content

        return converted_text

回答1:


Thanks MultipartPostHandler everything is working fine now.

class Post(Get): # inherits the Get class above
    def __init__(self,url,data,multipart=False):
        super(HTTP.Post,self).__init__(url)
        if multipart:
            from MultipartPostHandler import MultipartPostHandler
            self._add_handler(MultipartPostHandler)
            self.request.add_data(data)
        else:
            self.request.add_data(urlencode(data))


来源:https://stackoverflow.com/questions/4496691/uploading-file-using-urllib2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!