Skip to content
Myr

urllib2 Request subclass w/ settable methods

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 871de2a · Browse files · 1 file+39 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+39 added−9 removed

  • other:+39 added−9 removed

1 file+39 added−9 removed

modified:requests/core.py+39 added-9 removed
25 unmodified lines
2627AUTOAUTHS = []282930class _Request(urllib2.Request):31 def __init__(self, url, data=None, headers={},32 origin_req_host=None, unverifiable=False, method=None):33 urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)34 self.method = method3536 def get_method(self):37 if self.method:38 return self.method3940 return urllib2.Request.get_method(self)414243class Request(object):44 """The :class:`Request` object. It's awesome.45 """46
57 unmodified lines
104 else:105106 params = self.params10795 req = urllib2.Request("%s?%s" % (self.url, params))108 if self.method.lower() == 'get':109 req = _Request(("%s?%s" % (self.url, params)), method='GET')110 else:111 req = _Request(("%s?%s" % (self.url, params)), method='HEAD')11297 if self.method.lower() == 'head':98 req.get_method = lambda : 'HEAD'99100113 if self.headers:114 req.headers = self.headers115116 opener = self._get_opener()
12 unmodified lines
129130 elif self.method.lower() == 'put':131 if (not self.sent) or anyway:132 try:121 pass133134 req = _Request(self.url, method='PUT')135 req.data = self.data136 137# if self.method.lower() == 'head':138# req.get_method = lambda : 'PUT'139140 if self.headers:141 req.headers = self.headers142143 opener = self._get_opener()144 resp = opener(req)145146 self.response.status_code = resp.code147 self.response.headers = resp.info().dict148 if self.method.lower() == 'get':149 self.response.content = resp.read()150151 success = True152153125 except Exception:154 except RequestException:155 raise RequestException156 157 elif self.method.lower() == 'post':158 if (not self.sent) or anyway:
131 unmodified lines
290def put(url, data='', headers={}, auth=None):291 """Sends a PUT request. Returns :class:`Response` object.292 """293 r = Request()265 294295 r.url = url296 r.method = 'PUT'267 # return response object297 r.data = data298 299 r.headers = headers300 r.auth = _detect_auth(url, auth)301
50 unmodified lines