Skip to content

Commit 754d973

Browse files
committed
Upload some example code
1 parent c45c076 commit 754d973

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

get.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Performance Comparison between requests and pycurl
3+
'''
4+
import time
5+
import requests
6+
import pycurl
7+
from io import BytesIO
8+
9+
def pycurl_get():
10+
b = BytesIO()
11+
c = pycurl.Curl()
12+
c.setopt(c.URL, 'https://httpbin.org/ip')
13+
c.setopt(c.WRITEDATA, b)
14+
c.perform()
15+
c.close()
16+
body = b.getvalue()
17+
print('pycurl_get: %s' % body.decode('utf8'))
18+
19+
def requests_get():
20+
r = requests.get('https://httpbin.org/ip')
21+
print('requests_get: %s' % r.text)
22+
23+
def main():
24+
start_time = time.time()
25+
for i in range(100):
26+
requests_get()
27+
end_time = time.time()
28+
request_time = end_time - start_time
29+
30+
start_time = time.time()
31+
for i in range(100):
32+
pycurl_get()
33+
end_time = time.time()
34+
pycurl_time = end_time - start_time
35+
36+
print('The requests_get takes %f' % request_time)
37+
print('The pycurl_get takes %f' % pycurl_time)
38+
39+
40+
if __name__ == '__main__':
41+
main()

get_headers.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from io import BytesIO
2+
import requests
3+
import pycurl
4+
5+
headers = {}
6+
def header_function(header_line):
7+
# HTTP standard specifies that headers are encoded in iso-8859-1.
8+
# On Python 2, decoding step can be skipped.
9+
# On Python 3, decoding step is required.
10+
header_line = header_line.decode('iso-8859-1')
11+
12+
# Header lines include the first status line (HTTP/1.x ...).
13+
# We are going to ignore all lines that don't have a colon in them.
14+
# This will botch headers that are split on multiple lines...
15+
if ':' not in header_line:
16+
return
17+
18+
# Break the header line into header name and value.
19+
name, value = header_line.split(':', 1)
20+
21+
# Remove whitespace that may be present.
22+
# Header lines include the trailing newline, and there may be whitespace
23+
# around the colon.
24+
name = name.strip()
25+
value = value.strip()
26+
27+
# Header names are case insensitive.
28+
# Lowercase name here.
29+
name = name.lower()
30+
31+
# Now we can actually record the header name and value.
32+
headers[name] = value
33+
34+
def main():
35+
# Try Requests
36+
print('Use requests to get Google')
37+
res = requests.get('https://www.google.com')
38+
print('check headers')
39+
print(res.headers)
40+
print('check content:')
41+
print(res.text)
42+
print('-' * 20)
43+
# Try pycurl
44+
print('Use pycurl to get Google')
45+
b = BytesIO()
46+
c = pycurl.Curl()
47+
c.setopt(c.URL, 'https://www.google.com')
48+
c.setopt(c.HEADERFUNCTION, header_function)
49+
c.setopt(c.WRITEDATA, b)
50+
c.perform()
51+
print('check headers:')
52+
print(headers)
53+
print('check content:')
54+
print(b.getvalue())
55+
print('-' * 20)
56+
57+
if __name__ == '__main__':
58+
main()
59+
60+

get_with_cookies.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pycurl
2+
import requests
3+
from io import BytesIO
4+
from http.cookies import BaseCookie
5+
6+
def decode_cookie(raw_cookie):
7+
cookie = BaseCookie()
8+
cookie.load(raw_cookie)
9+
decoded_cookie = {}
10+
for c, v in cookie.items():
11+
decoded_cookie[c] = v.value
12+
return decoded_cookie
13+
14+
def requests_cookie_get():
15+
s = requests.session()
16+
decoded_cookie = decode_cookie('PUT ur raw cookie here.')
17+
res = s.get('https://www.google.com.tw/', cookies=decoded_cookie)
18+
res = s.get('https://www.google.com.tw/')
19+
print(res.text)
20+
21+
def requests_pycurl_get():
22+
b = BytesIO()
23+
c = pycurl.Curl()
24+
c.setopt(pycurl.URL, 'https://www.google.com.tw/')
25+
c.setopt(pycurl.COOKIE, 'Put Ur RAW Cookie here')
26+
c.setopt(pycurl.WRITEDATA, b)
27+
c.perform()
28+
29+
def main():
30+
requests_cookie_get()
31+
requests_pycurl_get()
32+
33+
if __name__ == '__main__':
34+
main()

0 commit comments

Comments
 (0)