Working with proxy
Working with proxy:
- create (POST /proxy)
- connect (POST /profile/set/proxy)
- start browser (POST /profile/profile_id/start)
Code
import requests
import json
def create_proxy(local_api_token, local_api_port, proxy):
url = f'http://localhost:{local_api_port}/proxy'
headers = {
'x-api-key': local_api_token
}
response = requests.post(url, headers=headers, json=proxy)
if response.status_code == 201:
return response.json()
else:
return {'error': 'Unable to fetch data', 'status_code': response.status_code}
def profile_set_proxy(local_api_token, local_api_port, profile_id, proxy_id):
url = f'http://localhost:{local_api_port}/profile/set/proxy'
headers = {
'x-api-key': local_api_token
}
body = {
"profileId": profile_id,
"proxyId": proxy_id
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 202:
return response.json()
else:
return {'error': 'Unable to fetch data', 'status_code': response.status_code}
def start_browser(local_api_token, local_api_port, profile_id):
url = f'http://localhost:{local_api_port}/profile/{profile_id}/start'
headers = {
'x-api-key': local_api_token
} # geolocationMode = One of ["allow", "block", "user"] # allow - browser automatically give a Geolocation info # block - browser automatically block Geolocation request # user - browser automatically show Geolocation request to user
body = {
"geolocationMode": "allow"
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
return response.json()
else:
return {'error': 'Unable to fetch data', 'status_code': response.status_code}
local_api_token = 'FA70fE'
local_api_port = 56789
# PLEASE CHANGE THIS, WRITE YOUR REAL PROXY
proxy = {
"host": "YOUR_HOST",
"port": 10001,
"name": "YOUR_PROXY_NAME",
"login": "LOGIN",
"password": "PASSWORD",
"protocol": "http",
"changeIpEndpoint": ""
}
new_proxy = create_proxy(local_api_token, local_api_port, proxy)
print("Create proxy")
print("-" \* 20)
print(json.dumps(new_proxy, indent=3))
proxy_id = new_proxy['_id']
profile_id = "6881275a8d96bb51aa4175e3"
res = profile_set_proxy(local_api_token, local_api_port, profile_id, proxy_id)
print("Set proxy")
print("-" \* 20)
print(json.dumps(res, indent=3))
res = start_browser(local_api_token, local_api_port, profile_id)
print("Start Browser")
print("-" \* 20)
print(json.dumps(res, indent=3))
Example of an answer
Create proxy
--------------------
{
"_id": "6881606b8d96bb51aa50a786",
"name": "MY_PROXY_NAME",
"protocol": "http",
"login": "LOGIN",
"password": "PASSWORD",
"host": "YOUR_HOST",
"port": PORT,
"changeIpEndpoint": ""
}
Set proxy
--------------------
{
"success": true
}
Start Browser
--------------------
{
"success": true,
"devToolsActivePort": 8745,
"status": "started"
}