-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurllib_demo.py
More file actions
52 lines (30 loc) · 905 Bytes
/
urllib_demo.py
File metadata and controls
52 lines (30 loc) · 905 Bytes
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
#!/usr/bin/python
from urllib.request import urlopen
url_pluralsight = 'http://pluralsight.com'
method = 'POST'
conn = urlopen(url_pluralsight)
print(type(conn))
#print(dir(conn))
print(conn.url)
print(conn.status)
print(conn.reason)
lines = conn.readlines()
#data = ''.join(lines)
print(lines[0])
data = ''.join(line.decode('utf-8') for line in lines)
print(data)
conn.close()
#http debuging
#https://hookbin.com/
url='https://hookb.in/vyxLVRbA'
method = 'POST'
data = b'{"message":"Hello World!"}'
headers = {'Content-Type': 'application/json'}
import urllib.request as q
httpRequest = q.Request(url, method=method, data=data, headers=headers)
httpConn = urlopen(httpRequest)
responseLines = ''.join([line.decode('utf-8') for line in httpConn.readlines()])
print(responseLines)
httpConn.close()
#r = http.request(method=method, url=url, headers=headers,body=data)
#print(r.read())