import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36')]
response = opener.open('http://www.baidu.com', timeout=10)
ip_port = response.fp.raw._sock.getpeername()