Skip to content

Instantly share code, notes, and snippets.

@rpi4gx
Created May 24, 2023 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpi4gx/ba62d3a350a198ee2d4807a00d86420e to your computer and use it in GitHub Desktop.
Save rpi4gx/ba62d3a350a198ee2d4807a00d86420e to your computer and use it in GitHub Desktop.
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()
@rpi4gx
Copy link
Author

rpi4gx commented May 24, 2023

To run it $ RAPID_API_KEY=_MY_KEY_HERE_ python3 ephemeral-proxies-sample.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment