Skip to content

urllib3 does not send http.client.connect audit events #2757

@apmorton

Description

@apmorton

Subject

Despite being implemented in terms of http.client, urllib3 doesn't cause http.client.connect audit events to be emitted

Steps to Reproduce

import http.client
import sys

import urllib3


def _hook(event: str, args: tuple):
    if event == "http.client.connect":
        print(event, args)
    if event == 'socket.connect':
        print(event, args)


sys.addaudithook(_hook)

print('using http.client')
http.client.HTTPConnection('www.python.org').connect()
print()
print('using urllib3')
http = urllib3.PoolManager()
resp = http.request("GET", "https://www.python.org")
using http.client
http.client.connect (<http.client.HTTPConnection object at 0x7f1303983fd0>, 'www.python.org', 80)
socket.connect (<socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 0)>, ('199.232.36.223', 80))

using urllib3
socket.connect (<socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 0)>, ('199.232.36.223', 443))

This is because the urllib3 HTTPConnection object overrides connect, which is where the audit events are sent from.
https://github.com/python/cpython/blob/e87ada48a9e5d9d03f9759138869216df0d7383a/Lib/http/client.py#L954

Expected Behavior

http.client.connect audit events should be emitted

Actual Behavior

They aren't

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions