Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to read data from a socket given a file descriptor

Reply
Thread Tools

how to read data from a socket given a file descriptor

 
 
Rajarshi Guha
Guest
Posts: n/a
 
      04-07-2004
Hi,
I'm writing some code using PyGTK2 that uses inpu_add_full() to watch a
socket. I've included the code below. I have two questions:

1) What does a condition == 16 mean? It does match any of the
gtk.gdk.INPUT_* values. What is the use of the condition variable in the
callback?

2) The source variable in the call back is an fd. I have some trivial code
that is able to read from a socket:

while 1:
sock.listen(1)
conn = sock.accept()[0]
got = conn.recv(20)
if (got == 'close'): break
conn.send('hello there %d' % (c))
c += 1

But how can I make this work when I get an fd?

Thanks,

--%<-------------------------------------------------------

def process_socket_message(source, condition):

if condition == gtk.gdk.INPUT_READ:
print 'something to read'
elif condition == gtk.gdk.INPUT_WRITE:
print 'ok to write'
elif condition == gtk.gdk.INPUT_EXCEPTION:
print 'an exception occured on the socket'
else:
print 'I dont know what the condition means'

conn = sock.accept()[0]
got = conn.recv(20)
print got
return gtk.TRUE

if __name__ == '__main__':

gnome.init('xxx','xxx')
widgets = WidgetsWrapper('gui.glade','toplevel', TopLevelHandlers)

# set up the socket to get messages from a client
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind('/tmp/mysocket')

# setup and input_add
socket_handler_id = gtk.input_add_full(sock, gtk.gdk.INPUT_READ, process_socket_message)

gtk.mainloop ()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to test if a given socket descriptor is valid? jungleman C++ 6 11-08-2011 11:29 PM
[socket] test socket descriptor state mbm C Programming 2 09-27-2007 02:48 PM
Read/Write IO on socket file descriptor Patrick LeBoutillier Perl Misc 1 07-20-2003 02:26 PM
Read/Write IO on socket file descriptor? Patrick LeBoutillier Perl 0 07-19-2003 05:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57