Save my name, email, and website in this browser for the next time I comment. This site uses Akismet to reduce spam. Learn how your comment data is processed. Skip to content. Nguyen Vu Ngoc Tung. No Comments. Table of Contents. Downloading Files in Python Using wget Module. Leave a Reply Cancel reply Your email address will not be published.
Related Posts. This is a mixin class that helps with HTTP authentication, both to the remote host and to a proxy. Handle authentication with the remote host. Handle authentication with the proxy. If the Digest Authentication returns a 40x response again, it is sent to Basic Authentication handler to Handle. This Handler method will raise a ValueError when presented with an authentication scheme other than Digest or Basic. It also defines several public attributes that can be used by clients to inspect the parsed request.
The URI path. If the Request uses a proxy, then selector will be the full URL that is passed to the proxy. The entity body for the request, or None if not specified. The HTTP request method to use. Return a string indicating the HTTP request method.
If Request. This is only meaningful for HTTP requests. Add another header to the request. Headers are currently ignored by all handlers except HTTP handlers, where they are added to the list of headers sent to the server. Note that there cannot be more than one header with the same name, and later calls will overwrite previous calls in case the key collides. Currently, this is no loss of HTTP functionality, since all headers which have meaning when used more than once have a header-specific way of gaining the same functionality using only one header.
Returns Request. Prepare the request by connecting to a proxy server. OpenerDirector instances have the following methods:. The following methods are searched, and added to the possible chains note that HTTP errors are a special case. See BaseHandler. Open the given url which can be a request object or a string , optionally passing the given data. Arguments, return values and exceptions raised are the same as those of urlopen which simply calls the open method on the currently installed global OpenerDirector.
Handle an error of the given protocol. This will call the registered error handlers for the given protocol with the given arguments which are protocol specific. Return values and exceptions raised are the same as those of urlopen. The order in which these methods are called within each stage is determined by sorting the handler instances. This stage ends when a handler either returns a non- None value ie. Exceptions are allowed to propagate.
BaseHandler objects provide a couple of methods that are directly useful, and others that are meant to be used by derived classes.
These are intended for direct use:. The following attribute and methods should only be used by classes derived from BaseHandler. A valid OpenerDirector , which can be used to open using a different protocol, or handle errors. This method is not defined in BaseHandler , but subclasses should define it if they want to catch all URLs. This method, if implemented, will be called by the parent OpenerDirector. It should return a file-like object as described in the return value of the open of OpenerDirector , or None.
This method is not defined in BaseHandler , but subclasses should define it if they want to handle URLs with the given protocol. This method, if defined, will be called by the parent OpenerDirector. This method is not defined in BaseHandler , but subclasses should define it if they want to catch all URLs with no specific registered handler to open it. This method is not defined in BaseHandler , but subclasses should override it if they intend to provide a catch-all for otherwise unhandled HTTP errors.
It will be called automatically by the OpenerDirector getting the error, and should not normally be called in other circumstances. Return values and exceptions raised should be the same as those of urlopen.
This method is also not defined in BaseHandler , but will be called, if it exists, on an instance of a subclass, when an HTTP error with code nnn occurs. This method is not defined in BaseHandler , but subclasses should define it if they want to pre-process requests of the given protocol. The return value should be a Request object. This method is not defined in BaseHandler , but subclasses should define it if they want to post-process responses of the given protocol.
The return value should implement the same interface as the return value of urlopen. See RFC for details of the precise meanings of the various redirection codes.
Return a Request or None in response to a redirect. The default implementation of this method does not strictly follow RFC , which says that and responses to POST requests must not be automatically redirected without confirmation by the user. In reality, browsers do allow automatic redirection of these responses, changing the POST to a GET , and the default implementation reproduces this behavior.
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I have made a program using urllib2 that makes a lot of connections across the web. I noticed that eventually that this can be DDoS worthy; I would like to know how to close down each connection after I have done my business to prevent such an attack.
I assume you are opening them with the urlopen function. Its documentation states:. The socket connection closes automatically, once the response is obtained.
So you don't explicitly close the urllopen object, it happens automatically at the socket level. How are we doing? Please help us improve Stack Overflow. Take our short survey. Note close releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown before close.
Alex Martelli answers to the similar question. Read this : should I call close after urllib. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Close urllib2 connection Ask Question. Asked 10 years, 8 months ago. Active 7 years, 9 months ago.
Viewed 14k times.
0コメント