forked from lexiforest/curl_cffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
49 lines (37 loc) · 1.25 KB
/
example.py
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
from io import BytesIO
from curl_cffi import Curl, CurlInfo, CurlOpt, requests
def main_curl():
buffer = BytesIO()
c = Curl()
c.setopt(CurlOpt.CUSTOMREQUEST, b"GET")
c.setopt(CurlOpt.URL, b"https://tls.browserleaks.com/json")
c.setopt(CurlOpt.WRITEDATA, buffer)
c.perform()
body = buffer.getvalue()
print("NO impersonate:")
print(body.decode())
print("")
buffer = BytesIO()
c.setopt(CurlOpt.WRITEDATA, buffer)
c.setopt(CurlOpt.URL, b"https://httpbin.org/headers")
c.impersonate("chrome99")
c.setopt(CurlOpt.HTTPHEADER, [b"User-Agent: Curl/impersonate"])
c.perform()
body = buffer.getvalue()
print("with impersonate:")
print(body.decode())
c.close()
def main_requests():
r = requests.get("https://tls.browserleaks.com/json")
print(r.json())
r = requests.get("https://tls.browserleaks.com/json", impersonate="chrome101")
print(r.json())
async def async_main():
async with requests.AsyncSession() as s:
r = await s.get("https://httpbin.org/headers")
print(r.text)
r = await s.get("https://httpbin.org/headers", stream=True)
async for content in r.iter_content():
print(content)
if __name__ == "__main__":
main_requests()