473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to read data from a socket given a file descriptor

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('hell o 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_R EAD:
print 'something to read'
elif condition == gtk.gdk.INPUT_W RITE:
print 'ok to write'
elif condition == gtk.gdk.INPUT_E XCEPTION:
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','to plevel', TopLevelHandler s)

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

# setup and input_add
socket_handler_ id = gtk.input_add_f ull(sock, gtk.gdk.INPUT_R EAD, process_socket_ message)

gtk.mainloop ()
Jul 18 '05 #1
0 1471

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5489
by: Rune | last post by:
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python v2.3 (Windows) I can use telnet host:port and enter the secret killword or use a broser with http://host:port/secret_killword The 'kill-server' validates the secret_killword and writes a file
1
10126
by: Patrick LeBoutillier | last post by:
Hi all, I'm trying to perform read and write I/O on a socket file descriptor received for another process via a Unix Domain Socket. In trying to understand all this I came up with a small test script that is not working for me: use strict ; use IO::Socket::INET ;
7
2463
by: Dario | last post by:
I have an unmanaged library that handle many TCP/IP connections. In my .NET application i want to test if there is input available on these connections. Using an existing function of the unmanaged library I'm able to retrieve the list of Int32 socket-descriptors handled by the unmanaged library. How can I use this list of Int32 socket-descriptors inside my C# program to test if there is input available on these socket-descriptors? I...
4
16630
by: GVK | last post by:
Hi, I'm facing another problem with my network program. I'm writing a basic server that handles multiple clients. Whenever a client is connected, a new thread is created to handle it. The socket descriptor of the client and it's sockaddr_in structure are passed as arguments to the function that handles the client. Now, the following code is giving me an error. It's the function that handles the client:...
5
2635
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server) that almost does the job. It listens on a user-defined port and responds to incoming data by writing how many times somebody has tried to connect to the server.
8
3370
by: M.Endraszka | last post by:
Hi, I am writing a simple irc-like server and came across weird behavior while debugging. Could anyone tell me why the following happens : I have a class which stores pipe descriptors as a member field. Descriptors are initialized with pipe(descr) in class constructor. Then I have an array of objects of this class, preferably a STL vector. If I initialize the vector with:
3
6252
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" ...
3
5403
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"...
2
1501
by: Dave13 | last post by:
Hi there Below is a small bit of test code for a project im working on, eventually the programs going to be a simple file transfer system, reading a file and then sending it out of a socket, but for now I cant get any files to open. I've been pouring over it for hours and I can't find up whats going wrong, the files there and has data in, but all the read() will return is -1 with a stderr message of Bad File Descriptor, but i just cant figure...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.