Specify source address on client connections

As in https://github.com/encode/httpx/issues/755, it would be useful to be able to specify the source address. I’m willing to work on a PR, if there’s any chance that something like this would be accepted.

I’m not sure exactly what the implementation in httpcore would look like, but it would likely involve setting an option on the SyncConnectionPool object, which passed it to SyncHTTPConnection, and then down to open_tcp_stream() (the async case is similar). The caller could pass in either the address either as a string, or a socket address tuple; if it was a string, it would be converted into a socket address. open_tcp_stream() would, if the source address was defined, call bind() on the socket.

Allowing this to be specified per-request would be more complicated, as that requires that the connection pool track connections by both origin and source address.

Yup we should absolutely support this. The place to start is httpcore, rather than httpx. We could then later push the configuration all the way through to httpx once that’s done.

I’d suggest chiming in on https://github.com/encode/httpcore/issues/88, and then perhaps taking a crack at a PR which adds the source address to AsyncConnectionPool.__init__/SyncConnectionPool.__init__ - can happily help you work through that.