问题
I am using Python 2.6.x and urllib2 to do some web scraping, but I need really low-level socket information (really just the port number of the local socket) for each HTTP request. Does anyone know how to get that?
Thanks
EDIT:
Okay, I'm still trying to get this right, so I did what I thought should work but I'm not getting the output when I try and use the new stuff. What am I doing wrong here?
from urllib2 import *
class AbstractHTTPHandler(AbstractHTTPHandler):
def do_open(self, http_class, req):
"""
...copy docstring...
"""
print "woot!"
...copy code from urllib2.AbstractHTTPHandler.do_open...
回答1:
urllib2 can operate on different URL schemes, which may not even have a notion of socket. Instead, use http.client's undocumented sock
property:
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/get-outgoing-port-number-from-urllib2