I need to set the timeout on urllib2.request().
I do not use urllib2.urlopen() since i am using the data parameter of request. How can I set this?
The getcode() method (Added in python2. 6) returns the HTTP status code that was sent with the response, or None if the URL is no HTTP URL.
The urllib. request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. See also. The Requests package is recommended for a higher-level HTTP client interface.
Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.
1) urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL. 2) urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.
Although urlopen does accept data param for POST, you can call urlopen on a Request object like this,
import urllib2 request = urllib2.Request('http://www.example.com', data) response = urllib2.urlopen(request, timeout=4) content = response.read()
still, you can avoid using urlopen and proceed like this:
request = urllib2.Request('http://example.com') response = opener.open(request,timeout=4) response_result = response.read() this works too :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With