Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketListener ignores peer address #839

Closed
adontz opened this issue Jan 5, 2019 · 4 comments
Closed

SocketListener ignores peer address #839

adontz opened this issue Jan 5, 2019 · 4 comments

Comments

@adontz
Copy link

adontz commented Jan 5, 2019

Currently SocketListener ignores peer address returned socket.accept

sock, _ = await self.socket.accept()

What is rationale behind this and what can be done to receive this information?

@njsmith
Copy link
Member

njsmith commented Jan 6, 2019

You can always get the peer address from the socket object itself by calling sock.getpeername().

Currently the "high level" Stream interface doesn't have a "high level" wrapper for this – fixing that is issue #280.

But, SocketStream's do expose the Trio socket object, so you can just call getpeername on that. Example:

import trio

async def server(stream):
    print("Got connection from:", stream.socket.getpeername())

trio.run(trio.serve_tcp, server, 12345)

And then if I do nc localhost 12345, the program prints something like:

Got connection from: ('127.0.0.1', 54888)

I don't think there's any further todo item for trio here, so marking this closed, but feel free to continue discussion or reopen if you want.

@njsmith
Copy link
Member

njsmith commented Jan 6, 2019

Oh, and if you're using serve_ssl_over_tcp, then the magic incantation is: stream.transport_stream.socket.getpeername()

@pquentin
Copy link
Member

pquentin commented Jan 6, 2019

I think @njsmith forgot to hit on the close button, so doing that now. Again, feel free to reopen!

@pquentin pquentin closed this as completed Jan 6, 2019
@njsmith
Copy link
Member

njsmith commented Jan 6, 2019

Whoops, thanks for the catch :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants