我正在使用Python2.6.x和urllib2进行一些web抓取,但是对于每个HTTP请求,我需要非常低级别的套接字信息(实际上只是本地套接字的端口号)。有人知道怎么弄吗?
谢谢
编辑:
好的,我还在努力把这个做好,所以我做了我认为应该有效的事情,但是当我尝试使用新的东西时,我没有得到输出。我在这里做错什么了?
from urllib2 import *
class AbstractHTTPHandler(AbstractHTTPHandler):
def do_open(self, http_class, req):
"""
...copy docstring...
"""
print "woot!"
...copy code from urllib2.AbstractHTTPHandler.do_open...发布于 2012-01-12 13:22:27
urllib2可以对不同的URL方案进行操作,这些方案甚至可能没有套接字的概念。相反,请使用http.client的无文档sock属性:
try:
from http.client import HTTPConnection
except ImportError: # Python<3
from httplib import HTTPConnection
h = HTTPConnection('example.net', 80)
h.request('GET', '/')
print('Local port: ' + str(h.sock.getsockname()[1]))https://stackoverflow.com/questions/8835577
复制相似问题