# Def fetch_url_requests(url):...

Exported on 05/04/2024 at 00:08:47 [from Phind Search](https://www.phind.com/search?cache=v0f2n8viy2xm2ntwnpgs1x9e) - with [SaveMyChatbot](https://save.hugocollin.com)

## User

def fetch_url_requests(url): """ Return the text of a url (using a proxy). """ print("fetch_url_requests: Sending request with requests...") # squid3 squid3_proxies = { "http": "http://127.0.0.1:3128", "https": "http://127.0.0.1:3128" } # polipo polipo_proxies = { "http": "http://127.0.0.1:8123", "https": "http://127.0.0.1:8123" } print("fetch_url_requests: url: " + url) r = requests.get(url, proxies=polipo_proxies) text = r.text print("fetch_url_requests: text: " + text) return text ----- i would like to rewrite to use requests-tor https://pypi.org/project/requests-tor/

## Answer

[1](https://stackoverflow.com/questions/30286293/make-requests-using-python-over-tor)

![favicon-stackoverflow.com](https://www.google.com/s2/favicons?domain=stackoverflow.com)stackoverflow.com \> questions \> 30286293 \> make-requests-using-python-over-tor

[Make requests using Python over Tor - Stack Overflow](https://stackoverflow.com/questions/30286293/make-requests-using-python-over-tor)

I want to make multiple GET requests using Tor to a webpage. I want to use a different ipaddress for each request. import socks import..

[2](https://pypi.org/project/requests-tor/)

![favicon-pypi.org](https://www.google.com/s2/favicons?domain=pypi.org)pypi.org \> project \> requests-tor

[requests-tor · PyPI](https://pypi.org/project/requests-tor/)

Multithreading requests via TOR with automatic TOR new identity. ... Dictionary mapping protocol to the URL of the proxy. verify – (optional) Either a boolean,..

[3](https://medium.com/@jasonrigden/using-tor-with-the-python-request-library-79015b2606cb)

![favicon-medium.com](https://www.google.com/s2/favicons?domain=medium.com)medium.com \> @jasonrigden \> using-tor-with-the-python-request-library-79015b2606cb

[Using Tor with the Python Requests Library - Medium](https://medium.com/@jasonrigden/using-tor-with-the-python-request-library-79015b2606cb)

Using Wireshark we can see the DNS requests for the previous examples. Example: requests.get ... So this is one way to use Tor with you..

[4](https://stackoverflow.com/questions/72506105/how-to-use-python-requests-with-proxies-over-tor)

![favicon-stackoverflow.com](https://www.google.com/s2/favicons?domain=stackoverflow.com)stackoverflow.com \> questions \> 72506105 \> how-to-use-python-requests-with-proxies-over-tor

[How to use python requests with proxies over Tor?](https://stackoverflow.com/questions/72506105/how-to-use-python-requests-with-proxies-over-tor)

tor runs as local proxy server. You can't connect from internet to your local server - you would have to redirect/forward port. But I don't..

Show more sources

To rewrite your function to use requests-tor instead of directly using proxies, you'll need to install the requests-tor package if you haven't already. You can do this by running pip install requests-tor in your terminal or command prompt.

The requests-tor package is designed to make HTTP requests over the Tor network, which can help anonymize your requests. It automatically handles the connection to the Tor network, so you don't need to manually specify proxies.

Here's how you can rewrite your fetch_url_requests function to use requests-tor:

```python

import requests_tor

def fetch_url_requests(url):
    """ Return the text of a url using requests-tor. """
    print("fetch_url_requests: Sending request with requests-tor...")
    print("fetch_url_requests: url: " + url)

    # Use requests_tor.get() instead of requests.get()
    r = requests_tor.get(url)

    # The response object is the same as the one returned by requests.get()
    text = r.text
    print("fetch_url_requests: text: " + text)
    return text
```

In this revised version of your function:

We import requests_tor instead of requests.
We use requests_tor.get(url) to make the HTTP request. This automatically routes the request through the Tor network, so you don't need to specify any proxies.
The rest of the function remains largely the same, as the response object returned by requests_tor.get() is compatible with the rest of your code.
This approach simplifies your code by removing the need to manually manage proxies. It also enhances the privacy and anonymity of your requests by routing them through the Tor network.

https://pypi.org/project/requests-tor/

## tor identities

To handle the Tor control port and manage new identities using requests-tor, you can follow the advanced usage instructions provided in the documentation. This involves specifying the Tor control port, setting a password if required, and configuring the automatic change of Tor identities after a certain number of requests.

Here's a step-by-step guide on how to do this:

1. Specify Tor Control Port: You need to specify the Tor control port using the tor_cport parameter. The default control port for Tor Browser is 9151, while for Tor itself, it's typically 9051.
2. Set a Password (Optional): If your Tor control port is password-protected, you can specify the password using the password parameter.
3. Automatic Change of Tor Identities: You can configure the number of requests after which a new Tor identity is automatically requested. This is done using the autochange_id parameter. Setting autochange_id to 0 disables automatic identity change.
4. Use Multiple Tor Ports (Optional): If you want to use multiple Tor ports, you can specify them using the tor_ports parameter. Requests will be sent sequentially through each port.
   Here's an example of how to configure these settings:

```python
from requests_tor import RequestsTor

# Initialize RequestsTor with custom settings
rt = RequestsTor(tor_ports=(9000, 9001, 9002, 9003, 9004), # Specify Tor socks ports
                 tor_cport=9051, # Specify Tor control port
                 password='your_password', # Specify Tor control port password
                 autochange_id=5, # Number of requests to change Tor identity
                 threads=8) # Number of threads for downloading URLs

# Check your IP
rt.check_ip()

# Request a new Tor identity
rt.new_id()

# Make a GET request
response = rt.get('http://example.com')

# Process the response as needed
print(response.text)

```

In this example:

- We specify multiple Tor socks ports for load balancing or to use different Tor circuits.
- We set the Tor control port to 9051, which is the default for Tor.
- We provide a password for the Tor control port.
- We configure the automatic change of Tor identities after every 5 requests.
- We use 8 threads for downloading URLs concurrently.
  Remember to replace 'your_password' with your actual Tor control port password if you have one. If your Tor control port does not require a password, you can omit the password parameter or set it to None.
  This setup allows you to make requests over Tor with automatic management of Tor identities, enhancing your privacy and anonymity while making HTTP requests [2](https://github.com/deedy5/requests_tor) [3](https://iphelix.medium.com/hacking-the-tor-control-protocol-fb844db6a606)
