A Response object.
>>> import urlfetch
>>> response = urlfetch.get("http://docs.python.org/")
>>> response.total_time
0.033042049407959
>>> response.status, response.reason, response.version
(200, 'OK', 10)
>>> type(response.body), len(response.body)
(<type 'str'>, 8719)
>>> type(response.text), len(response.text)
(<type 'unicode'>, 8719)
>>> response.getheader('server')
'Apache/2.2.16 (Debian)'
>>> response.getheaders()
[
('content-length', '8719'),
('x-cache', 'MISS from localhost'),
('accept-ranges', 'bytes'),
('vary', 'Accept-Encoding'),
('server', 'Apache/2.2.16 (Debian)'),
('last-modified', 'Tue, 26 Jun 2012 19:23:18 GMT'),
('connection', 'close'),
('etag', '"13cc5e4-220f-4c36507ded580"'),
('date', 'Wed, 27 Jun 2012 06:50:30 GMT'),
('content-type', 'text/html'),
('x-cache-lookup', 'MISS from localhost:8080')
]
>>> response.headers
{
'content-length': '8719',
'x-cache': 'MISS from localhost',
'accept-ranges': 'bytes',
'vary': 'Accept-Encoding',
'server': 'Apache/2.2.16 (Debian)',
'last-modified': 'Tue, 26 Jun 2012 19:23:18 GMT',
'connection': 'close',
'etag': '"13cc5e4-220f-4c36507ded580"',
'date': 'Wed, 27 Jun 2012 06:50:30 GMT',
'content-type': 'text/html',
'x-cache-lookup': 'MISS from localhost:8080'
}
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
Generate a Response object from a httplib response object.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
read content (for streaming and large files)
chunk_size: size of chunk, default: 8192
Reason phrase returned by server.
Status code returned by server.
A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
total time
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
A session object.
urlfetch.Session can hold common headers and cookies. Every request issued by a urlfetch.Session object will bring u these headers and cookies.
urlfetch.Session plays a role in handling cookies, just like a cookiejar.
Parameters: |
|
---|
pack a session and write packed bytes to fileobj
>>> import urlfetch
>>> s = urlfetch.Session({'User-Agent': 'urlfetch'}, {'foo': 'bar'})
>>> f = open('session.jar', 'wb')
>>> s.dump(f)
>>> f.close()
Parameters: |
|
---|
pack a seesion and return packed bytes
>>> import urlfetch
>>> s = urlfetch.Session({'User-Agent': 'urlfetch'}, {'foo': 'bar'})
>>> s.dumps()
...
Parameters: | cls (string, marshal, pickle, etc...) – use which class to pack the session |
---|---|
Return type: | packed bytes |
unpack a session from fileobj and load it into current session
>>> import urlfetch
>>> s = urlfetch.Session()
>>> f = open('session.jar', 'rb')
>>> s.load(f)
>>> f.close()
Parameters: |
|
---|---|
Return type: | unpacked session |
unpack a seesion from string and load it into current session
>>> import urlfetch
>>> s = urlfetch.Session({'User-Agent': 'urlfetch'}, {'foo': 'bar'})
>>> s.loads(s.dumps())
{'headers': {'User-Agent': 'urlfetch'}, 'cookies': {'foo': 'bar'}}
Parameters: |
|
---|---|
Return type: | unpacked session |
Remove an cookie from default cookies
Add an cookie to default cookies
request an URL
Parameters: |
|
---|---|
Return type: | A Response object |
fetch an URL.
fetch() is a wrapper of request(). It calls get() by default. If one of parameter data or parameter files is supplied, post() is called.
Issue a GET request
Issue a POST request
Issue a HEAD request
Issue a PUT request
Issue a DELETE request
Issue a OPTIONS request
Issue a TRACE request
Issue a PATCH request
returns dictionary of parsed url: scheme, netloc, path, params, query, fragment, uri, username, password, host and port
Convert Set-Cookie header to cookie string.
Set-Cookie can be retrieved from a Response instance:
sc = response.getheader('Set-Cookie')
Parameters: | sc – (string) Set-Cookie |
---|---|
Return type: | cookie string, which is name=value pairs joined by ;. |
Returns a User-Agent string randomly from file.
>>> ua = random_useragent('file1')
>>> ua = random_useragent('file1', 'file2')
>>> ua = random_useragent(['file1', 'file2'])
>>> ua = random_useragent(['file1', 'file2'], 'file3')
Parameters: | filename (string, optional) – path to the file from which a random useragent is generated |
---|
Imports an object by name.
import_object(‘x.y.z’) is equivalent to ‘from x.y import z’.
>>> import_object('os.path') is os.path
True
>>> import_object('os.path.dirname') is os.path.dirname
True
Concatenate url and argument dictionary
>>> url_concat("http://example.com/foo?a=b", dict(c="d"))
'http://example.com/foo?a=b&c=d'
Parameters: |
|
---|