forked from fipl-hse/2021-2-level-ctlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_requests.py
74 lines (56 loc) · 2.22 KB
/
try_requests.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
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
import random
import time
import requests
from requests import Timeout, HTTPError
if __name__ == '__main__':
# 1. requests basics
CORRECT_URL = 'https://pypi.org/project/requests/'
INCORRECT_URL = f'{CORRECT_URL}garbagegarbage'
response = requests.get(CORRECT_URL)
if response:
print(f'Response code is: {response.status_code}')
if response.ok:
print('Response is OK')
try:
response = requests.get(INCORRECT_URL)
response.raise_for_status()
except HTTPError as e:
print(f'Error occurred: {e.response.status_code}')
# 2. requests advanced
# 2.1 handling timeouts, need to specify it for long responding servers
try:
response = requests.get(INCORRECT_URL, timeout=0.000001)
except Timeout as e:
print(f'In timeout: {e}')
# 2.2 making pauses between requests to make them look more natural for a server
response = requests.get(CORRECT_URL)
sleep_period = 10
print(f'Sleeping for {sleep_period}')
time.sleep(sleep_period)
response = requests.get(CORRECT_URL)
sleep_period = random.randrange(3, 7)
print(f'Sleeping for {sleep_period}')
time.sleep(sleep_period)
response = requests.get(CORRECT_URL)
# 2.3 specifying browser headers to make a request look more natural for a server
response = requests.get(CORRECT_URL)
print(response.request.headers)
print(response.headers)
response = requests.get(CORRECT_URL, headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
})
print(response.request.headers)
print(response.headers)
# 3. working with responses
# 3.1 getting HTML page content as a plain Python string
response = requests.get(CORRECT_URL)
print(response.text)
# 3.2 saving HTML page as a file
response = requests.get(CORRECT_URL)
with open('index.html', 'w', encoding='utf-8') as f:
f.write(response.text)
# 3.3 saving binary files formats, such as images
response = requests.get('https://pypi.org/static/images/logo-small.95de8436.svg')
with open('logo.svg', 'wb') as f:
f.write(response.content)