-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccurl.py
33 lines (26 loc) · 873 Bytes
/
ccurl.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
#!Python
#coding=gbk
__version__ = "$Id$"
import cclogger
import urllib, urllib2
# 中文注释
import sys
reload(sys)
sys.setdefaultencoding("gbk")
def buildRequest(url, params, headers = {}):
"""构造一个request"""
return urllib2.Request(url, urllib.urlencode(params), headers)
def tryOpenRequest(request):
"""打开一个Request并返回Response,遇到异常将退出应用"""
response = None
logger = cclogger.buildLogger()
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
logger.info("打开 " + request.get_full_url() + " 失败,HTTP错误:" + str(e.code))
except urllib2.URLError, e:
logger.info("打开 " + request.get_full_url() + " 失败,网络连接错误:" + str(e.reason[0]) + " " + e.reason[1])
if response:
return response
else:
exit()