Get Outgoing Port Number From Urllib2
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.
Solution 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]))
Post a Comment for "Get Outgoing Port Number From Urllib2"