Get outgoing port number from urllib2

核能气质少年 提交于 2019-12-11 09:58:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!