Repository navigation
Expand file tree
/
Copy pathhttp.py
More file actions
63 lines (56 loc) · 1.58 KB
/
Copy pathhttp.py
File metadata and controls
63 lines (56 loc) · 1.58 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import urllib
import urllib2
import json
import os
# HTTP library
# HTTP Delete wrapper
def httpDelete(url):
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(url)
request.get_method = lambda: 'DELETE'
try:
ret = opener.open(request)
return ret
except urllib2.HTTPError, err:
if err.code == 404:
print "Page not found!"
elif err.code == 403:
print "Access denied!"
else:
print "HTTP Error response! Error code", err.code
return "Error"
except urllib2.URLError, err:
print "URL error:", err.reason
return "Error"
# HTTP POST wrapper
def httpPost(url, data):
try:
retData = urllib2.urlopen(url, data)
return retData.read()
except urllib2.HTTPError, err:
if err.code == 404:
print "Page not found!"
elif err.code == 403:
print "Access denied!"
else:
print "HTTP Error! Error code", err.code
return "Error"
except urllib2.URLError, err:
print "URL error:", err.reason
return "Error"
# Wrapper for HTTP get
def httpGet(url):
try:
retData = urllib2.urlopen(url)
return retData.read()
except urllib2.HTTPError, err:
if err.code == 404:
print "Page not found!"
elif err.code == 403:
print "Access denied!"
else:
print "HTTP Error! Error code", err.code
return "Error"
except urllib2.URLError, err:
print "URL error:", err.reason
return "Error"