Hi,
I'm trying to find the best and most reliable way to get connection info - specifically remote host/ip and port - within any of the callbacks (ex. on receive, on send, on connect, on disconnect) for a link object.
Ex. say I have some function that I'm using for the on-connect callback:
def my_connect_callback(conn, ident):
print 'Client %s connected' % ident
# not much else I can do
If I want to know who is connecting from within this function, using the two arguments made available to me (conn and ident) and some link object mylinkobject, can I do this?
(host, port) = mylinkobject.get_socket_by_conn(conn).sock.getpeername()
So my callback function could be improved by changing it to:
def my_connect_callback(conn, ident):
(host, port) = mylinkobject.get_socket_by_conn(conn).sock.getpeername()
print 'Client %s connected from %s:%d' % (ident, host, port)
I dont think get_socket_by_conn is documented but I found it in link.py. Is this an alright way to grab this remote host/port info from the provided 'conn' argument? Or should I be doing this lookup differently? It's been working fine in my testing but I may be missing something big here.
Thanks!
-Jason