Accessing connection details within callback function, using conn id

29 views
Skip to first unread message

Jason Beyers

unread,
Jul 24, 2013, 7:07:00 PM7/24/13
to sna...@googlegroups.com
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




Jason Beyers

unread,
Jul 24, 2013, 7:14:19 PM7/24/13
to sna...@googlegroups.com
Ah, I did find a mention of get_socket_by_conn in the docs for getting peer cert info, I must have missed that.  So I'm assuming this approach is acceptable.

David Široký

unread,
Jul 25, 2013, 9:14:44 AM7/25/13
to sna...@googlegroups.com
Hi!

get_socket_by_conn(conn) returns an instance of LinkSocket class and it has the sock attribute which is an instance of Python's socket or SSLSocket. You can safely use it.

David
Reply all
Reply to author
Forward
0 new messages