-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcustom_headers.py
More file actions
executable file
·238 lines (197 loc) · 7.43 KB
/
custom_headers.py
File metadata and controls
executable file
·238 lines (197 loc) · 7.43 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
"""
Custom Headers and User-Agent Example
This example demonstrates how to customize HTTP headers with CycleTLS:
- Setting custom User-Agent strings
- Adding custom headers
- Controlling header ordering
- Using order_headers_as_provided flag for exact header control
"""
import cycletls
def basic_custom_headers_example(client):
"""
Demonstrate basic custom headers usage.
"""
print("\n" + "="*60)
print("Example 1: Basic Custom Headers")
print("="*60)
try:
# Make request with custom headers
response = client.get(
"https://httpbin.org/headers",
headers={
"Accept": "application/json",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Custom-Header": "CustomValue123",
"X-API-Key": "secret-api-key-12345"
},
user_agent="MyCustomBot/2.0 (compatible; Example/1.0)"
)
print(f"\nStatus Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print("\nHeaders received by server:")
for key, value in data["headers"].items():
print(f" {key}: {value}")
print("\n✓ Custom headers sent successfully!")
else:
print(f"✗ Request failed with status {response.status_code}")
except Exception as e:
print(f"✗ Error: {e}")
def header_ordering_example(client):
"""
Demonstrate controlling header order with header_order parameter.
Some websites may detect bots based on header ordering, so CycleTLS
allows you to specify the exact order in which headers should appear.
"""
print("\n" + "="*60)
print("Example 2: Custom Header Ordering")
print("="*60)
try:
# Define custom header order (common Chrome browser order)
custom_header_order = [
"host",
"connection",
"cache-control",
"sec-ch-ua",
"sec-ch-ua-mobile",
"sec-ch-ua-platform",
"upgrade-insecure-requests",
"user-agent",
"accept",
"sec-fetch-site",
"sec-fetch-mode",
"sec-fetch-user",
"sec-fetch-dest",
"accept-encoding",
"accept-language"
]
response = client.get(
"https://httpbin.org/headers",
headers={
"Accept": "text/html,application/xhtml+xml",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "max-age=0",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
},
header_order=custom_header_order,
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0"
)
print(f"\nStatus Code: {response.status_code}")
print(f"Custom header order: {custom_header_order[:5]}...")
if response.status_code == 200:
print("\n✓ Request with custom header ordering successful!")
else:
print(f"✗ Request failed with status {response.status_code}")
except Exception as e:
print(f"✗ Error: {e}")
def order_as_provided_example(client):
"""
Demonstrate the order_headers_as_provided flag.
When set to True, headers will be sent in the exact order they appear
in the headers dictionary (Python 3.7+ maintains insertion order).
"""
print("\n" + "="*60)
print("Example 3: Order Headers As Provided")
print("="*60)
try:
# Headers will be sent in this exact order
ordered_headers = {
"X-Custom-First": "First header",
"Accept": "application/json",
"X-Custom-Second": "Second header",
"Authorization": "Bearer token123",
"X-Custom-Third": "Third header",
"Content-Type": "application/json"
}
response = client.post(
"https://httpbin.org/post",
headers=ordered_headers,
order_headers_as_provided=True,
body='{"test": "data"}',
user_agent="OrderedHeaderBot/1.0"
)
print(f"\nStatus Code: {response.status_code}")
print("\nHeaders sent (in order):")
for key in ordered_headers.keys():
print(f" {key}: {ordered_headers[key]}")
if response.status_code == 200:
print("\n✓ Headers sent in exact provided order!")
else:
print(f"✗ Request failed with status {response.status_code}")
except Exception as e:
print(f"✗ Error: {e}")
def browser_impersonation_example(client):
"""
Demonstrate full browser impersonation with headers, user-agent, and JA3.
This combines multiple features to closely mimic a real browser.
"""
print("\n" + "="*60)
print("Example 4: Full Browser Impersonation")
print("="*60)
# Chrome 120 configuration
chrome_config = {
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1"
},
"ja3": "771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0"
}
try:
response = client.get(
"https://httpbin.org/headers",
**chrome_config
)
print(f"\nImpersonating: Chrome 120 on Windows")
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"\nServer detected User-Agent:")
print(f" {data['headers'].get('User-Agent', 'N/A')}")
print("\n✓ Full browser impersonation successful!")
else:
print(f"✗ Request failed with status {response.status_code}")
except Exception as e:
print(f"✗ Error: {e}")
def main():
"""
Main function to run all custom header examples.
"""
print("CycleTLS Custom Headers and User-Agent Examples")
print("=" * 60)
# Create CycleTLS client
print("\nInitializing CycleTLS client...")
client = cycletls.CycleTLS()
try:
# Run all examples
basic_custom_headers_example(client)
header_ordering_example(client)
order_as_provided_example(client)
browser_impersonation_example(client)
print("\n" + "="*60)
print("✓ All custom header examples completed!")
print("="*60)
except Exception as e:
print(f"\n✗ Fatal error: {e}")
raise
finally:
# Clean up
print("\nClosing CycleTLS client...")
client.close()
print("✓ Done!")
if __name__ == "__main__":
main()