本文整理汇总了Python中urllib2.addinfourl函数的典型用法代码示例。如果您正苦于以下问题:Python addinfourl函数的具体用法?Python addinfourl怎么用?Python addinfourl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了addinfourl函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: check_zip
def check_zip(self, resp):
"""
@Def: check_zip
@Description: check web page if it's ziped
@Input: resp #response of web
@Output: data #web page data
@Date: 2015-8-10
"""
if resp.headers.get("content-encoding") == "gzip":
lib.log_print.log_print_debug("Zip page.")
data = resp.read()
gz = gzip.GzipFile(fileobj=StringIO.StringIO(data))
new_resp = urllib2.addinfourl(gz, resp.headers, resp.url, resp.code)
new_resp.msg = resp.msg
return new_resp
elif resp.headers.get("content-encoding") == "deflate":
lib.log_print.log_print_debug(" Deflate page. url: %s" % (resp.geturl()))
data = resp.read()
gz = StringIO.StringIO(self._deflate(data))
new_resp = urllib2.addinfourl(gz, resp.headers, resp.url, resp.code)
new_resp.msg = resp.msg
return new_resp
else:
lib.log_print.log_print_debug(
" Unzip page. Encoding type: %s, url: %s"
% (str(resp.headers.get("content-encoding")), resp.geturl())
)
return resp
开发者ID:pandadady,项目名称:mini-spider,代码行数:28,代码来源:crawler.py
示例2: open_local_file
def open_local_file(self, req):
import email.Utils
host = req.get_host()
file = req.get_selector()
localfile = url2pathname(file)
stats = os.stat(localfile)
size = stats.st_size
modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
mtype = mimetypes.guess_type(file)[0]
headers = mimetools.Message(StringIO(
'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
(mtype or 'text/plain', size, modified)))
if host:
host, port = splitport(host)
if not host or \
(not port and socket.gethostbyname(host) in self.get_names()):
try:
file_list = dircache.listdir(localfile)
s = StringIO()
s.write('<html><head><base href="%s"/></head><body>' % ('file:' + file))
s.write('<p>Directory Content:</p>')
for f in file_list:
s.write('<p><a href="%s">%s</a></p>\n' % (urllib.quote(f), f))
s.write('</body></html>')
s.seek(0)
headers = mimetools.Message(StringIO(
'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
('text/html', size, modified)))
return addinfourl(s, headers, 'file:' + file)
except OSError:
return addinfourl(open(localfile, 'rb'),
headers, 'file:'+file)
raise URLError('file not on local host')
开发者ID:smetsjp,项目名称:erp5,代码行数:33,代码来源:ContributionOpener.py
示例3: GetLastUpdateTime
def GetLastUpdateTime():
last_update = None
url = 'http://hq.sinajs.cn/rn=1389255083862&list=gb_dji'
data = ''
user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0'
headers = { 'User-Agent' : user_agent,
'Host': 'hq.sinajs.cn',
'Accept': '*/*',
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
#'Connection': 'keep-alive',
'Referer': 'http://finance.sina.com.cn/stock/usstock/sector.shtml',
}
req = urllib2.Request(url, data, headers)
resp = urllib2.urlopen(req)
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
gz = GzipFile(
fileobj=StringIO(resp.read()),
mode="r"
)
#resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url)
resp.msg = old_resp.msg
#json_html = gz.read()
#print 'xxx'
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO( deflate(resp.read()) )
#resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url) # 'class to add info() and
resp.msg = old_resp.msg
#json_html = gz.read()
#print 'YY'
js_script = resp.read()
#print js_script
last_update_time = js_script.split(',')[25]
#print last_update_time
#Jan 08 04:13PM EST
ridx = last_update_time.rfind('EST')
if ridx == -1:
#try:
raise ValueError('Timezone EST value not set')
#except ValueError, e:
# print e
else:
last_update_time = last_update_time[:ridx].strip()
print last_update_time
last_update = datetime.datetime.strptime(last_update_time, "%b %d %I:%M%p")
last_update = last_update.replace(datetime.date.today().year)
print last_update
return last_update
开发者ID:lidong1266,项目名称:SmartSTrader,代码行数:56,代码来源:get_from_sina.py
示例4: http_response
def http_response(self, request, response):
old_response = response
if response.headers.get("content-encoding") == "gzip":
gzipFile = gzip.GzipFile(fileobj=StringIO.StringIO(response.read()),mode="r")
response = urllib2.addinfourl(gzipFile, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
if response.headers.get("content-encoding") == "deflate":
gzipFile = StringIO.StringIO(deflate(response.read()) )
response = urllib2.addinfourl(gzipFile, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
return response
开发者ID:ElninoFong,项目名称:r10,代码行数:11,代码来源:crdownloader.py
示例5: http_response
def http_response(self, request, response):
old_response = response
if response.headers.get('Content-Encoding') == 'gzip':
gz = gzip.GzipFile(fileobj=StringIO.StringIO(response.read() ), mode='r')
response = urllib2.addinfourl(gz, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
if response.headers.get('Content-Encoding') == 'deflate':
gz = StringIO.StringIO(zlib.decompressobj(-zlib.MAX_WBITS).decompress(response.read() ) )
response = urllib2.addinfourl(gz, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
return response
开发者ID:npk,项目名称:pcbeta-robots,代码行数:11,代码来源:pcbeta-robots.py
示例6: http_response
def http_response(self, req, resp):
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
gz = gzip.GzipFile( fileobj=BytesIO(resp.read()), mode="r" )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = BytesIO( self.deflate(resp.read()) )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
开发者ID:pk13610,项目名称:vavava,代码行数:12,代码来源:httputil.py
示例7: http_response
def http_response(self, req, resp):
old_resp = resp
# gzip
if resp.headers.get('content-encoding') == 'gzip':
gz = GzipFile(fileobj=BytesIO(resp.read()), mode='r')
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get('content-encoding') == 'deflate':
gz = BytesIO( deflate(resp.read()) )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp.msg = old_resp.msg
return resp
开发者ID:liveonnet,项目名称:postgetter-app,代码行数:13,代码来源:PostGetter.py
示例8: http_response
def http_response(self, req, resp):
old_resp = resp
# gzip
if resp.headers.get("content-encoding") == "gzip":
gz = GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO(deflate(resp.read()))
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp.msg = old_resp.msg
return resp
开发者ID:raycool,项目名称:web_login,代码行数:13,代码来源:login.py
示例9: http_open
def http_open(self, req):
url = req.get_full_url()
try:
content = self.urls[url]
except KeyError:
resp = urllib2.addinfourl(StringIO(""), None, url)
resp.code = 404
resp.msg = "OK"
else:
resp = urllib2.addinfourl(StringIO(content), None, url)
resp.code = 200
resp.msg = "OK"
return resp
开发者ID:thatguystone,项目名称:webassets,代码行数:13,代码来源:test_bundle.py
示例10: http_response
def http_response(self, req, resp):
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
data = gzip.GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(data, old_resp.headers,
old_resp.url, old_resp.code)
resp.msg = old_resp.msg
if resp.headers.get("content-encoding") == "deflate":
data = zlib.decompress(resp.read(), -zlib.MAX_WBITS)
resp = urllib2.addinfourl(data, old_resp.headers,
old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
开发者ID:infinet,项目名称:lunar-calendar,代码行数:13,代码来源:lunar_ical.py
示例11: mock_response
def mock_response(self, req):
if req.get_full_url() == "http://localhost/test":
resp = urllib2.addinfourl(StringIO("mock file"), "mock message", req.get_full_url())
resp.code = 200
resp.msg = "OK"
return resp
if req.get_full_url() == "http://localhost/typo":
resp = urllib2.addinfourl(StringIO("mock file"), "mock message", req.get_full_url())
resp.code = 404
resp.msg = "Not Found"
return resp
else:
raise SystemExit, 2
开发者ID:jrottenberg,项目名称:check_jenkins,代码行数:13,代码来源:test_check_jenkins.py
示例12: test_ignore_non_index_etag
def test_ignore_non_index_etag(self):
""" Etags are ignored for non-index requests """
req = urllib2.Request('http://foo.com/')
res = urllib2.addinfourl(StringIO('foo'), {'Etag': 'whatevs'}, 'http://foo.com/')
self.cache_handler.http_response(req, res)
self.assertFalse(os.path.exists(self.metadata_path))
self.assertFalse(os.path.exists(self.index_path))
开发者ID:agrawalprash,项目名称:enstaller,代码行数:7,代码来源:test_cached.py
示例13: test_no_etag
def test_no_etag(self):
""" No Etag header == no cache written """
req = urllib2.Request('http://foo.com/index.json')
res = urllib2.addinfourl(StringIO('foo'), {}, 'http://foo.com/index.json')
self.cache_handler.http_response(req, res)
self.assertFalse(os.path.exists(self.metadata_path))
self.assertFalse(os.path.exists(self.index_path))
开发者ID:agrawalprash,项目名称:enstaller,代码行数:7,代码来源:test_cached.py
示例14: http_response
def http_response(self, req, resp):
old_resp = resp
if not req.has_header('Accept-encoding'):
return resp
if req.has_header('Range'):
return resp
if resp.headers.get("content-encoding") == "gzip":
gz = _GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO(self.deflate(resp.read()))
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
开发者ID:pkhopper,项目名称:vavava,代码行数:16,代码来源:httputil.py
示例15: http_open
def http_open(self, req):
req_url = req.get_full_url()
file_path = req_url.replace(base_http_url, base_file_path)
resp = urllib2.addinfourl(open(file_path), "redirected to file", req.get_full_url())
resp.code = 200
resp.msg = "OK"
return resp
开发者ID:robspinella,项目名称:streetsblog-calendar,代码行数:7,代码来源:utils.py
示例16: http_open
def http_open(self, req):
requests.append(dict(
url=req.get_full_url(),
contenttype=req.headers['Content-type'],
auth=base64.decodestring(req.headers['Authorization'].replace('Basic ', '')),
data=json.loads(req.data),
))
# Sanitise response
global nextResponse
if 'id' not in nextResponse:
nextResponse['id'] = requests[-1]['data']['id']
if 'error' not in nextResponse:
nextResponse['error'] = None
if 'result' not in nextResponse:
nextResponse['result'] = 1234
resp = urllib2.addinfourl(
StringIO(nextResponse['_data'] if '_data' in nextResponse else json.dumps(nextResponse)),
"Message of some form",
req.get_full_url(),
)
resp.code = nextResponse['_code'] if '_code' in nextResponse else 200
resp.msg = nextResponse['_msg'] if '_msg' in nextResponse else "OK"
nextResponse = dict()
return resp
开发者ID:tutor-web,项目名称:tutorweb.quizdb,代码行数:26,代码来源:test_coin.py
示例17: http_response
def http_response(self, _, resp):
if resp.headers.get("content-encoding") == "gzip":
gz = GzipFile(fileobj=StringIO(resp.read()), mode="r")
old_resp = resp
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
开发者ID:ayanamist,项目名称:TwiOtaku,代码行数:7,代码来源:urlfetch.py
示例18: mock_response
def mock_response(req):
url= req.get_full_url().split('/')[-1]
if url in URLS.keys():
resp = urllib2.addinfourl(StringIO.StringIO(URLS[url]), "mock message", req.get_full_url())
resp.code = 200
resp.msg = "OK"
return resp
开发者ID:pombredanne,项目名称:python-dlipower,代码行数:7,代码来源:test_dlipower.py
示例19: test_urlopen_success
def test_urlopen_success(self):
response = urllib2.addinfourl(self.fp, self.headers, self.url)
response.code = 200
response.msg = "OK"
with mock.patch('urllib2.urlopen') as mock_urlopen:
mock_urlopen.return_value = response
urlopen(self.url)
开发者ID:Knewton,项目名称:k.stdlib,代码行数:7,代码来源:test_urlopen_error_message.py
示例20: test_get_swarm_results_success
def test_get_swarm_results_success(self):
self.mox.StubOutWithMock(swarm_results.urllib2, 'urlopen')
shard_output = json.dumps(
{'machine_id': 'host',
'machine_tag': 'localhost',
'exit_codes': '0, 0',
'output': SWARM_OUTPUT_WITHOUT_FAILURE
}
)
url_response = urllib2.addinfourl(StringIO.StringIO(shard_output),
"mock message", 'host')
url_response.code = 200
url_response.msg = "OK"
swarm_results.urllib2.urlopen('http://host:9001/get_result?r=key1'
).AndReturn(url_response)
swarm_results.urllib2.urlopen('http://host:9001/cleanup_results',
data=urllib.urlencode({'r': 'key1'})
).AndReturn(StringIO.StringIO(''))
self.mox.ReplayAll()
swarm_results.GetSwarmResults('http://host:9001', ['key1'])
self.checkstdout(BUILDBOT_OUTPUT)
self.mox.VerifyAll()
开发者ID:Acidburn0zzz,项目名称:build,代码行数:27,代码来源:get_swarm_results_test.py
注:本文中的urllib2.addinfourl函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论