-
-
Save rpi4gx/ba62d3a350a198ee2d4807a00d86420e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
def obtain_proxy(): | |
url = "https://ephemeral-proxies.p.rapidapi.com/v2/datacenter/proxy" | |
headers = { | |
"X-RapidAPI-Key": os.environ.get('RAPID_API_KEY'), | |
"X-RapidAPI-Host": "ephemeral-proxies.p.rapidapi.com" | |
} | |
response = requests.get(url, headers=headers) | |
return response.json() | |
def main(): | |
# Obtain a proxy from the API | |
api_response = obtain_proxy() | |
print(api_response) | |
# Set up the proxy configuration | |
proxy_host = api_response['proxy']['host'] | |
proxy_port = api_response['proxy']['port'] | |
proxy_url = f'http://{proxy_host}:{proxy_port}' | |
proxy_config = { | |
'http': proxy_url, | |
'https': proxy_url | |
} | |
# Visit a website using the proxy obtained | |
try: | |
response = requests.get('https://ifconfig.co/ip', proxies=proxy_config) | |
if response.status_code == 200: | |
print('Response:') | |
print(response.text) | |
else: | |
print(f'Request failed with status code: {response.status_code}') | |
except requests.exceptions.RequestException as e: | |
print(f'Request failed with error: {e}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run it
$ RAPID_API_KEY=_MY_KEY_HERE_ python3 ephemeral-proxies-sample.py