How to use Socks5 proxy in Requests library
introductory
When you see these words, are you a bit confused? Don't worry, let's talk about how to use Socks5 proxy in Requests library today. Speaking of proxies, I believe we all know what it does. Simply put, a proxy is an intermediary used to replace your access to a website, it will help you hide the real IP address, to protect your privacy. Socks5 proxy, which is a more advanced type of proxy, allows us to be more flexible and comfortable network access. So, let's learn how to use Socks5 proxy in Python's Requests library!
Step 1: Install the Socks5 proxy library
Before using Socks5 proxy, we first need to install a Python library that supports Socks5 proxy. Here we recommend a library called "PySocks", which provides a series of convenient utilities to help us easily implement the Socks5 proxy function. To install the PySocks library, simply run the following command from the command line:
pip install PySocks
Step 2: Send a request using the Socks5 proxy
Okay, now that we have installed the Socks5 proxy library, the next step is to use it to send HTTP requests. In the Requests library, we can set the "proxies" parameter to specify the proxy address and port number. The code is as follows:
import requests
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1080)
socket.socket = socks.socksocket
response = requests.get("https://www.example.com")
print(response.text)
In the above code, we first call the "set_default_proxy" function to set the global Socks5 proxy address and port number. Here I take the localhost address as an example, and the port number is set to 1080. Then, we replace the original socket class with the Socks5 socket class via "socks.socksocket", so that the Requests library will automatically use the proxy when sending requests. so that the Requests library will automatically use the proxy when sending requests. Finally, we use the "requests.get" function to send a GET request, specifying the URL to access, and the returned response is stored in the "response" variable. We can view the content of the response through the "response.text".
Step 3: Verify that the proxy is in effect
In order to verify that the Socks5 proxy we set up is working, we can use some methods to see the source IP of the request. here, I demonstrate it in a convenient way, using a free IP query interface. We can modify the above code to include the URL of the IP query interface:
import requests
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1080)
socket.socket = socks.socksocket
response = requests.get("http://ip-api.com/json")
print(response.json()["query"])
In the code, I have changed the access URL to "ip-api.com/json" which returns the IP address of the current request. Through the "response.json()["query"]" we can parse out the returned results and print them out. After running the code, you will find that the printed IP address is different from your real IP address, which means that the proxy setting is successful and your request has gone through the Socks5 proxy.
summarize
With these steps, we learned how to use Socks5 proxy in Python's Requests library. After mastering this technique, you can be more flexible in network access and protect your privacy. I hope this article is helpful to you, if you have any questions, please feel free to leave a comment!