[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&nbsp;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>&nbsp;</DIV>
<DIV><FONT size=2>My programs is follows,named 'ddd.c':</FONT></DIV>
<DIV><FONT color=#0000ff size=2>#include &lt;stdio.h&gt;<BR>#include 
&lt;error.h&gt;<BR>#include &lt;sys/types.h&gt;<BR>#include 
&lt;netinet/ip.h&gt;<BR>#include &lt;netinet/tcp.h&gt;<BR>#include 
&lt;netinet/udp.h&gt;<BR>#include &lt;linux/ioctl.h&gt;<BR>#include 
&lt;linux/if_ether.h&gt;<BR>#include &lt;linux/if_arp.h&gt;</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>#define MAXLINE 4096<BR>ssize_t n;<BR>union 
{<BR>&nbsp; unsigned int addr;<BR>&nbsp; unsigned char 
ip[4];<BR>}saddr,daddr;</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>char&nbsp; * mysock()<BR>{<BR>&nbsp; int 
listenfd;<BR>&nbsp; char buff[MAXLINE];<BR>&nbsp; 
if((listenfd=socket(AF_INET,SOCK_PACKET,htons(ETH_P_IP)))&lt;0)<BR>&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp; perror("create 
socket(SOCK_PACKET)\n");<BR>&nbsp;&nbsp;&nbsp; exit(-1);<BR>&nbsp; }<BR>&nbsp; 
if ((n=recv(listenfd,buff,MAXLINE,0))&gt;0)<BR>&nbsp;&nbsp;&nbsp; return 
buff;<BR>&nbsp;<BR>}</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>#include "Python.h"</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>static PyObject * my_mysock(PyObject 
*self,PyObject *args)<BR>{<BR>&nbsp; int num;<BR>&nbsp; 
if(!PyArg_ParseTuple(args,"i",&amp;num))<BR>&nbsp;&nbsp;&nbsp; return 
NULL;<BR>&nbsp; return (PyObject *)Py_BuildValue("s",mysock());<BR>}&nbsp;&nbsp; 
</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>static PyMethodDef 
myMethods[]={<BR>&nbsp;{"mysock",my_mysock,METH_VARARGS},<BR>&nbsp;{NULL,NULL},<BR>};</FONT></DIV>
<DIV><FONT color=#0000ff></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff size=2>void initmy()<BR>{<BR>&nbsp; 
Py_InitModule("reads",myMethods);<BR>}&nbsp;&nbsp;&nbsp;<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>&gt;&gt;&gt;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>&nbsp; File "&lt;stdin&gt;",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>&nbsp;</DIV>
<DIV><FONT size=2>How to correct? The return value&nbsp;'<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Thank you&nbsp;very much!!!</FONT>&nbsp;</DIV></FONT></DIV></BODY></HTML>

--Boundary_(ID_QS6it3QBclAVjXxe7vw6pA)--