-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (102 loc) · 4.5 KB
/
main.py
File metadata and controls
134 lines (102 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Browserbase Proxy Testing Script - See README.md for full documentation
import json
import os
from browserbase import Browserbase
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright
from pydantic import BaseModel, Field
from stagehand import Stagehand
load_dotenv()
bb = Browserbase(api_key=os.environ.get("BROWSERBASE_API_KEY"))
class GeoInfo(BaseModel):
"""Schema for IP information and geolocation data"""
ip: str = Field(..., description="The IP address")
city: str = Field(..., description="The city name")
region: str = Field(..., description="The state or region")
country: str = Field(..., description="The country code")
loc: str = Field(..., description="The latitude and longitude coordinates")
timezone: str = Field(..., description="The timezone")
org: str = Field(..., description="The organization or ISP")
postal: str = Field(..., description="The postal code")
hostname: str = Field(..., description="The hostname if available")
def create_session_with_built_in_proxies():
# Use Browserbase's default proxy rotation for enhanced privacy and IP diversity.
session = bb.sessions.create(
proxies=True,
)
return session
def create_session_with_geo_location():
# Route traffic through specific geographic location to test location-based restrictions.
session = bb.sessions.create(
proxies=[
{
"type": "browserbase",
"geolocation": {
"city": "NEW_YORK",
"state": "NY",
"country": "US",
},
}
],
)
return session
def create_session_with_custom_proxies():
# Use external proxy servers for custom routing or specific proxy requirements.
session = bb.sessions.create(
proxies=[
{
"type": "external",
"server": "http://...",
"username": "user",
"password": "pass",
}
],
)
return session
def test_session(session_function, session_name: str):
print(f"\n=== Testing {session_name} ===")
# Create session with specific proxy configuration
session = session_function()
session_id = session.id
print(f"Session URL: https://browserbase.com/sessions/{session_id}")
# Initialize Stagehand with Browserbase for cloud-based browser automation
client = Stagehand(
browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"),
)
try:
# Connect to the browser via CDP
with sync_playwright() as playwright:
browser = playwright.chromium.connect_over_cdp(
f"wss://connect.browserbase.com?apiKey={os.environ['BROWSERBASE_API_KEY']}&sessionId={session_id}"
)
context = browser.contexts[0] if browser.contexts else None
if not context:
raise Exception("No default context found")
page = context.pages[0] if context.pages else None
if not page:
raise Exception("No page found in default context")
# Navigate to IP info service to verify proxy location and IP address.
page.goto("https://ipinfo.io/json", wait_until="domcontentloaded")
# Extract structured IP and location data using Stagehand
extract_response = client.sessions.extract(
id=session_id,
instruction="Extract all IP information and geolocation data from the JSON response",
schema=GeoInfo.model_json_schema(),
)
print("Geo Info:", json.dumps(extract_response.data.result, indent=2))
browser.close()
client.sessions.end(id=session_id)
print(f"{session_name} test completed")
except Exception as error:
print(f"Error during Stagehand extraction: {error}")
client.sessions.end(id=session_id)
def main():
# Test 1: Built-in proxies - Verify default proxy rotation works and shows different IPs.
test_session(create_session_with_built_in_proxies, "Built-in Proxies")
# Test 2: Geolocation proxies - Confirm traffic routes through specified location (New York).
test_session(create_session_with_geo_location, "Geolocation Proxies (New York)")
# Test 3: Custom external proxies - Enable if you have a custom proxy server set up.
# test_session(create_session_with_custom_proxies, "Custom External Proxies")
print("\n=== All tests completed ===")
if __name__ == "__main__":
main()