FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
proxy-examples/python/requests-session-proxy.py at main · proxymesh/proxy-examples · GitHub
proxymesh
/
proxy-examples
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
4
Code
Issues
4
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
proxy-examples
/
python
/
requests-session-proxy.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (28 loc) · 1.2 KB
Breadcrumbs
proxy-examples
/
python
/
requests-session-proxy.py
Copy path
File metadata and controls
37 lines (28 loc) · 1.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python3
"""
Requests Session with an HTTP proxy.
Configuration via environment variables:
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
RESPONSE_HEADER - Optional header name to print from the response
Uses a :class:`requests.Session` for connection pooling. Same proxy options as
``requests.get(..., proxies=...)``.
Documentation: https://docs.python-requests.org/en/latest/user/advanced/#proxies
"""
import
os
import
sys
import
requests
proxy_url
=
os
.
environ
.
get
(
'PROXY_URL'
)
or
os
.
environ
.
get
(
'HTTPS_PROXY'
)
if
not
proxy_url
:
print
(
'Error: Set PROXY_URL environment variable'
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
test_url
=
os
.
environ
.
get
(
'TEST_URL'
,
'https://api.ipify.org?format=json'
)
response_header
=
os
.
environ
.
get
(
'RESPONSE_HEADER'
)
proxies
=
{
'http'
:
proxy_url
,
'https'
:
proxy_url
}
with
requests
.
Session
()
as
session
:
session
.
proxies
.
update
(
proxies
)
response
=
session
.
get
(
test_url
,
timeout
=
30
)
print
(
f'Status:
{
response
.
status_code
}
'
)
print
(
f'Body:
{
response
.
text
}
'
)
if
response_header
:
print
(
f'
{
response_header
}
:
{
response
.
headers
.
get
(
response_header
)
}
'
)
Back
|
FazBrowse Home
|
New Git URL