473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

embedded python doesn't like socket.accept() and SegFaults

Hi everyone,

I'm practicing with embedding python into C code and i have encountered
a very strange problem: I'm unable to call the "accept" method of a
(correctly created) server socket without receiving a "Segmentation
fault" (inside the PyObject_CallMethod).

My code <seemsto be correct (at least it's correct enough for me to
call .getsockname(), .fileno() and other methods without problems), I'm
pretty new to this thing though, therefore I'm confident I'm doing
something very dumb.

Here is the C code:

------ extending.c
#include <python2.5/Python.h>

PyObject *new_server(long port)
{
PyObject *pName,*pModule;
PyObject *pFunction,*pArg;
PyObject *pTuple,*pValue;

// Import the module
pName = PyString_FromString("extending");
pModule = PyImport_Import(pName);
Py_DECREF(pName);

// Get the function
pFunction = PyObject_GetAttrString(pModule,
"server_socket");

// Module not needed anymore
Py_DECREF(pModule);

// Build the arguments
pArg=PyInt_FromLong(port);
pTuple=PyTuple_New(1);
PyTuple_SET_ITEM(pTuple,0,pArg);

// Call the function
pValue = PyObject_CallObject(pFunction,
pTuple);

// Release the references
Py_DECREF(pFunction);
Py_DECREF(pTuple);

if(pValue==NULL)
printf("Error: server socket not created!\n");

return pValue;
}

PyObject *accept(PyObject *server)
{
PyObject *pValue;

// Code fails here (it does NOT return NULL: just crashes!)
// Note that other calls work fine (e.g. fileno, getsockname ecc)
pValue = PyObject_CallMethod(server,
"accept",
NULL);
return pValue;
}

int main(int argc,char *argv[])
{
PyObject *server,*connection;

// Boot python
Py_Initialize();
PySys_SetArgv(argc, argv);

// Create the server
server=new_server(23000);

// Print it
PyObject_Print(server,stderr,0);
fprintf(stderr,"\n");

// Wait for a connection
connection=accept(server);

// See what we got
PyObject_Print(connection,stderr,0);
fprintf(stderr,"\n");

// We are done, hint the gc.
Py_DECREF(connection);
Py_DECREF(server);

Py_Finalize();
return 0;
}
----------

and this is the python script:

---------- extending.py
import socket

def server_socket(port):
s=socket.socket()
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR ,True)
s.bind(("0.0.0.0",port))
s.listen(10)
return s
------------

as already mentioned, replacing the "accept" string with "fileno" or
something else doesn't crash the interpreter.

Another thing worth mentioning, is that even inserting a s.accept() call
in the python script (before the return) makes the bug appear (it
doesn't seems related to my use of the PyObject_CallMethod function, then).

I have tried posting the problem in IRC, searching google (no good
matches) and debugging the code (however I'm afraid i don't have the
python-lib with debugging syms. compiled in, therefore it was quite a
useless attempt...).

Any help about the code would be appreciated (even unrelated to the
issue at hand: im quite new to this "embedding thing" and therefore i
gladly accept hints).

Thank you for your attention,
Riccardo Di Meo

PS: I'm also new to Usenet: is it fine to post the code in the body of
the mail as i did (since it was small, i dared: however I'd like to know
the correct etiquette)?

Aug 2 '08 #1
0 1307

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

Similar topics

4
by: Edmond Rusjan | last post by:
Hi All, I'd like to use Python-2.3.4 on OSF1 V4.0, but have trouble installing. With a plain "./configure; make" build, I cannot import socket. If I uncomment the socketmodule in Modules/Setup,...
5
by: wahn | last post by:
Hi, Here is a problem I came across and need some help with. I developed a little Python script with some classes which runs standalone and communicates with a database via sockets. So far...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
6
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please...
8
by: Giampaolo Rodola' | last post by:
I'm not sure if this is a question about python programming, system administration or sockets in general... I have the FTP server in my signature to which I'd want to add IPv6 support. My hosting...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
0
by: Riccardo Di Meo | last post by:
Solved (with the help of the guys on #python on freenode). Long story short: i forgot the static in the function definitions and the libc's "accept" got replaced with mine... Riccardo Di...
0
by: GeicoGecko | last post by:
Hey guys, There was no python section in the "Web Development" forum so I'm hoping I can pose my question in here. Our system We currently have a python webserver using SimpleHTTPServer,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.