Home python Using ProxyHandler to send requests

Using ProxyHandler to send requests




created a function that in the infinite loop sends a request to the server. In front of the cycle launched a proxy.

Interested in: Will the URLOPEN function be used at each iteration to use a proxy, or for this, the creation of ProxyHandler also need to be placed in the cycle?

Answer 1, Authority 100%

install_opener () has a global effect for the entire Python process. Inside the function, it is better explicitly Opener.open () Use instead of urllib2.urlopen () – at the same time there will be no doubt if Opener or not.

Is the proxy used or not for urllib2.urlopen () may depend on whether someone has called in another install_opener () stream with other parameters. It cannot be said that “will”, without seeing the code for the entire application.

also the result may depend on the protocol (HTTP, HTTPS, FTP). Even this option is possible that the initial request goes through proxy, and subsequent requests, after redirection (for example, with another protocol) may not use proxy.

I am not very strong in Python, an example of a proxy in the function found on the expanses of the Internet. Can I somehow code briefly tell me how it should look like?

In the case of multiple threads, you do not need to use install_opener () and urllib2.urlopen (). And create your own Opener in each thread:

Import Time
Import UrLLIB2.
Def Open_Website (URL, Proxies):
  Opener = UrLLIB2.Build_opener (URLLIB2.ProxyHandler (Proxies))
  While True:
      Opener.open (URL) CLOSE ()
    Except Exception AS E:
      Print (E)
    Time.Sleep (0.4)

Yes, there is still a substantial nuance, I don’t need an answer from the server, it is only an opportunity to send a request, I found in the internet that I will just help urllib2.urlopen ()

Urlopen () reads only the headers from the server and does not read the contents of the answer.

Answer 2, Authority 33%

Interested in: Will the URLOPEN function, with each iteration, will use a proxy, or for this, the creation of ProxyHandler should also be placed in the cycle?

will be.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions