-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api3.py
More file actions
44 lines (40 loc) · 1.51 KB
/
Copy pathtest_api3.py
File metadata and controls
44 lines (40 loc) · 1.51 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
import requests
import sys
def test_api():
url = 'https://api.qweather.com/geo/v2/city/lookup'
params = {'location': '北京', 'key': 'cfb037bde7de452e8c5f8b15d741fb31', 'lang': 'zh'}
print('Testing with key parameter...')
try:
r = requests.get(url, params=params, timeout=15)
print('Status:', r.status_code)
print('Headers:', dict(r.headers))
print('Response body:', r.text)
print('Response encoding:', r.encoding)
except Exception as e:
print('Error:', e)
import traceback
traceback.print_exc()
print('\n--- Testing with X-QW-Api-Key header ---')
headers = {'X-QW-Api-Key': 'cfb037bde7de452e8c5f8b15d741fb31'}
params2 = {'location': '北京', 'lang': 'zh'}
try:
r2 = requests.get(url, headers=headers, params=params2, timeout=15)
print('Status:', r2.status_code)
print('Response:', r2.text)
except Exception as e:
print('Error:', e)
import traceback
traceback.print_exc()
print('\n--- Testing weather now with known location ID ---')
url3 = 'https://api.qweather.com/v7/weather/now'
params3 = {'location': '101010100', 'key': 'cfb037bde7de452e8c5f8b15d741fb31', 'lang': 'zh'}
try:
r3 = requests.get(url3, params=params3, timeout=15)
print('Status:', r3.status_code)
print('Response:', r3.text)
except Exception as e:
print('Error:', e)
import traceback
traceback.print_exc()
if __name__ == '__main__':
test_api()