AttributeError: ‘WrappedSocket‘ object has no attribute ‘getsockname‘ or ‘getpeername‘

The following exceptions were caught when using requests to obtain the client and server IP addresses and ports:

'WrappedSocket' object has no attribute 'getsockname'
'WrappedSocket' object has no attribute 'getpeername'

Previously, it was an HTTP request, and the address and port number could be obtained normally. After changing it to HTTPS, an exception was thrown.

Found the solution on stackoverflow.

Address: https://stackoverflow.com/questions/22492484/how-do-i-get-the-ip-address-from-a-http-request-using-the-requests-library

plan:

try:
	server_ip, server_port = response.raw.connection.sock.getpeername()
except AttributeError as ex:
	# logger.warning(f"failed to get server address info: {ex}")
	server_ip, server_port = response.raw.connection.sock.socket.getpeername()
logger.debug(f"server IP: {
      
      server_ip}, \tPort: {
      
      server_port}")

Get the same for the client.

Guess you like

Origin blog.csdn.net/lan_yangbi/article/details/122562767