forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.py
More file actions
57 lines (41 loc) · 1.59 KB
/
net.py
File metadata and controls
57 lines (41 loc) · 1.59 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Operations that involve networking.
Running this may take some time or fail.
"""
import urllib2
if "##POST request":
'''
##urlopen
Does the actual request.
Raises urllib2.URLError on error.
It timeout not given a global default is used.
Retruns bytestring, not unicode.
Encoding can sometimes be inferred from Content-Type HTTP header:
Content-Type: text/html; charset=ISO-8859-1
Returns a file like object with some extra methods.
info() returns an object with methods shown in: <http://docs.python.org/2/library/mimetools.html#mimetools.Message>
'''
print "GitHub Markdown:"
url = 'https://api.github.com/markdown'
data = '{"text":"**Hello World 你好**", "mode":"gfm", "context":"github/gollum"}'
req = urllib2.Request(url, data)
# Custom request headers;
#request = urllib2.Request("http://www.google.com", headers={"Accept" : "text/html"})
try:
response = urllib2.urlopen(req, timeout=10)
except urllib2.URLError, e:
print e
sys.exit(1)
response_body = response.read()
assert type(response_body) == str
print 'response body = ' + response_body
print 'response status = {}'.format(response.getcode())
info = response.info()
# Header string list
print 'response info = ' + repr(info.headers)
# Header key val dict
print 'response info = ' + repr(info.items())
##retry
# Exponential backoff retry: http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/