Quick start
Use a proxy with Python
A complete, runnable Python example with Requests: proxies, timeouts, retries, country and sessions.
This guide is part of the ProxyOnly quick-start series. It assumes nothing and takes a few minutes. Prices are live, and you can start with a balance of one US dollar in any of the 63 countries we stock.
- Guide
- Use a proxy with Python
- Series
- 13 quick-start guides
- Minimum balance
- US$1
The minimum that works
Python's Requests library accepts a dictionary of proxies. Because ProxyOnly proxies are HTTP proxies, both the http and the https keys use an http:// proxy URL. The example fetches an IP echo page and prints the address.
import requests
PROXY = "http://username-country-us:[email protected]:8000"
proxies = {"http": PROXY, "https": PROXY}
response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=20)
print(response.status_code, response.json())Make it robust
A script that runs once is not the same as a script that runs for an hour. Put the proxy on a Session so that connections are reused, set a timeout on every call, and retry failures a few times with a pause that grows. Reusing the connection saves a lot of time on large jobs.
import time, requests
session = requests.Session()
session.proxies = {"http": PROXY, "https": PROXY}
def fetch(url, tries=3):
for attempt in range(tries):
try:
r = session.get(url, timeout=20)
if r.status_code == 200:
return r
except requests.RequestException:
pass
time.sleep(2 ** attempt)
return NoneCountry and session
Build the username from the options you want. A function that creates the URL for a country and a session name keeps this tidy and makes it easy to give each worker its own sticky session.
def proxy_url(country="us", session=None):
user = f"username-country-{country}"
if session:
user += f"-session-{session}"
return f"http://{user}:[email protected]:8000"Common Python pitfalls
Requests reads proxy settings from environment variables such as HTTP_PROXY and HTTPS_PROXY too, so a forgotten variable can change behaviour without any code change. Passing proxies explicitly makes the behaviour clear. If you use concurrency, give each thread or task its own session or protect a shared one, and keep the number of parallel requests to a single site small.
Check response.status_code before you parse. A blocked request often returns a 403 or 429 page with perfectly valid HTML, and a parser will happily read it.
Which proxy type fits any country
Pick the proxy type from the job rather than the price. Here is how the types we sell compare, so you can match one to your work in any country:
Datacenter IPv4. The default choice for most jobs. These are dedicated IPv4 addresses hosted in a datacenter, fast and inexpensive, and the best fit for high-volume reading of public pages, monitoring and automation. Some websites treat datacenter ranges more cautiously than consumer connections, so test your target before you commit to a long rental.
ISP proxies. Static addresses that are registered to an internet service provider rather than a hosting company. They keep the speed of datacenter hardware and look more like an ordinary home or office connection, which helps with destinations that are strict about hosting ranges.
Shared IPv4. A shared address is used by more than one customer and costs less. It suits light tasks, quick checks and testing where a clean, exclusive address is not essential.
IPv6. IPv6 addresses are very cheap and plentiful, and they work well with destinations that serve IPv6 traffic, such as many large search, video and social platforms. Many smaller sites are still IPv4 only, so check that your target supports IPv6 before you buy.
Mobile proxies. Addresses from mobile carrier networks. Many phones share the same carrier address, so websites tend to be tolerant of this traffic, which makes mobile proxies the right choice for testing mobile ads, apps and landing pages. They cost more than datacenter addresses.
Residential proxies. Addresses from home internet connections, billed by the amount of traffic you use and not by the IP. They suit light, bursty jobs on sites that block datacenter and ISP ranges. Heavy traffic gets expensive quickly, so estimate your monthly volume first.
If you are unsure, rent one address of the type you think you need, run your real task against it for a day, and look at the success rate. Because rental periods start at a few days, trying a type costs very little.
Using proxies responsibly in any country
A proxy is a networking tool and it is lawful in most places, but what you do with it still has to be lawful and fair. Follow the laws that apply to you and to the place you send traffic to, read the terms of the sites you visit, honour robots rules and rate limits, and never use an address to access accounts or data that are not yours. ProxyOnly cannot give legal advice, and our acceptable use policy applies to every order.
In practice that means reading public pages, checking your own sites and ads, and testing your own products. If a task would be unwelcome to the site owner when done by hand, it is unwelcome when done through a proxy too.
Frequently asked questions
Which Python library should I use?
Requests is the simplest. httpx and aiohttp are good for asynchronous work. All of them accept an http proxy URL with credentials.
Why do I use http:// for https sites?
The proxy itself is an HTTP proxy. It opens a tunnel for HTTPS traffic and cannot read it.
How do I use a different country per request?
Build the username with the country you want, for example username-country-de, and create a new proxy URL.
How do I avoid being blocked?
Lower concurrency, add pauses, send realistic headers and respect the site's rules. Consider an ISP proxy for strict sites.
Ready to try it?
Create an account, add funds from US$1 and pick your country and proxy type.