[Tutor] help
ÕÅÉÙ³Û
zhangsc@neusoft.com
Mon, 05 Nov 2001 15:51:00 +0800
This is a multi-part message in MIME format.
--Boundary_(ID_QS6it3QBclAVjXxe7vw6pA)
Content-type: text/plain; charset=gb2312
Content-transfer-encoding: 7BIT
I want to extending python, so I write a program. My purpose is return a buff from my program,so I can receive the buff in Python,the buff's type is char buff[MAXLINE] .I don't know whether Python's extending support char buff[MAXLINE] type?
My programs is follows,named 'ddd.c':
#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <linux/ioctl.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#define MAXLINE 4096
ssize_t n;
union {
unsigned int addr;
unsigned char ip[4];
}saddr,daddr;
char * mysock()
{
int listenfd;
char buff[MAXLINE];
if((listenfd=socket(AF_INET,SOCK_PACKET,htons(ETH_P_IP)))<0)
{
perror("create socket(SOCK_PACKET)\n");
exit(-1);
}
if ((n=recv(listenfd,buff,MAXLINE,0))>0)
return buff;
}
#include "Python.h"
static PyObject * my_mysock(PyObject *self,PyObject *args)
{
int num;
if(!PyArg_ParseTuple(args,"i",&num))
return NULL;
return (PyObject *)Py_BuildValue("s",mysock());
}
static PyMethodDef myMethods[]={
{"mysock",my_mysock,METH_VARARGS},
{NULL,NULL},
};
void initmy()
{
Py_InitModule("reads",myMethods);
}
My extending steps are followed:
$gcc ddd.c -c -I /usr/include/python1.5
$ld -share -o mymodule.so ddd.o
$python
>>>import my
__main__:1: RuntimeWarning:Python C API version mismatch for module reads:
Python has API version 1011,module reads has version 1007.
File "<stdin>",line 1,in ?
SystemError:_PyImport_FixupExtension: module my not loaded
How to correct? The return value 'char buff[MAXLINE]' is very important for me, it will affect
my next work,Would you help me?
Thank you very much!!!
--Boundary_(ID_QS6it3QBclAVjXxe7vw6pA)
Content-type: text/html; charset=gb2312
Content-transfer-encoding: 7BIT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2>
<DIV><FONT size=2>I want to extending python, so I write a program. My
purpose is return a buff from my program,so I can receive the buff in Python,the
buff's type is <FONT color=#0000ff>char buff[MAXLINE]</FONT> .I don't know
whether Python's extending support char buff[MAXLINE] type?</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>My programs is follows,named 'ddd.c':</FONT></DIV>
<DIV><FONT color=#0000ff size=2>#include <stdio.h><BR>#include
<error.h><BR>#include <sys/types.h><BR>#include
<netinet/ip.h><BR>#include <netinet/tcp.h><BR>#include
<netinet/udp.h><BR>#include <linux/ioctl.h><BR>#include
<linux/if_ether.h><BR>#include <linux/if_arp.h></FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>#define MAXLINE 4096<BR>ssize_t n;<BR>union
{<BR> unsigned int addr;<BR> unsigned char
ip[4];<BR>}saddr,daddr;</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>char * mysock()<BR>{<BR> int
listenfd;<BR> char buff[MAXLINE];<BR>
if((listenfd=socket(AF_INET,SOCK_PACKET,htons(ETH_P_IP)))<0)<BR>
{<BR> perror("create
socket(SOCK_PACKET)\n");<BR> exit(-1);<BR> }<BR>
if ((n=recv(listenfd,buff,MAXLINE,0))>0)<BR> return
buff;<BR> <BR>}</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>#include "Python.h"</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>static PyObject * my_mysock(PyObject
*self,PyObject *args)<BR>{<BR> int num;<BR>
if(!PyArg_ParseTuple(args,"i",&num))<BR> return
NULL;<BR> return (PyObject *)Py_BuildValue("s",mysock());<BR>}
</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>static PyMethodDef
myMethods[]={<BR> {"mysock",my_mysock,METH_VARARGS},<BR> {NULL,NULL},<BR>};</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff size=2>void initmy()<BR>{<BR>
Py_InitModule("reads",myMethods);<BR>} <BR><FONT
color=#000000></FONT></FONT></DIV>
<DIV><FONT color=#0000ff size=2><FONT color=#000000>My extending steps are
followed:</FONT></FONT></DIV>
<DIV><FONT color=#0000ff size=2>$gcc ddd.c -c -I
/usr/include/python1.5</FONT></DIV>
<DIV><FONT color=#0000ff size=2>$ld -share -o mymodule.so ddd.o</FONT></DIV>
<DIV><FONT color=#0000ff size=2>$python</FONT></DIV>
<DIV><FONT color=#ff0000 size=2>>>>import my</FONT></DIV>
<DIV><FONT color=#ff0000 size=2>__main__:1: RuntimeWarning:Python C API version
mismatch for module reads:</FONT></DIV>
<DIV><FONT color=#ff0000 size=2>Python has API version 1011,module reads has
version 1007.</FONT></DIV>
<DIV><FONT color=#ff0000 size=2> File "<stdin>",line 1,in
?</FONT></DIV>
<DIV><FONT color=#ff0000 size=2>SystemError:_PyImport_FixupExtension: module my
not loaded</FONT></DIV>
<DIV><FONT color=#ff0000 size=2></FONT> </DIV>
<DIV><FONT size=2>How to correct? The return value '<FONT
color=#0000ff>char buff[MAXLINE]' </FONT><FONT color=#000000>is very important
for me, it will affect </FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#000000>my next work,Would you help
me?</FONT></FONT></DIV>
<DIV><FONT
size=2>
Thank you very much!!!</FONT> </DIV></FONT></DIV></BODY></HTML>
--Boundary_(ID_QS6it3QBclAVjXxe7vw6pA)--