Python and C language Mixed programming instance _python

Source: Internet
Author: User
Tags mixed socket

Recently, in order to test the speed of the situation, because some business servers need to shut down ICMP, so that the use of ordinary ping can not adapt to my needs, so I wrote a simple TCP port based on the program, because C implementation efficiency is relatively good, but inefficient development, While Python is highly efficient, but less efficient than C, because of the need for large-scale use, so use C to implement the core parts of the code, and the implementation of this part into a Python module, by Python call C module, the following code

Copy Code code as follows:

* TCPPORTPING.C * *
#include <Python.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>

/* Count Time functions * *
static double MyTime (void)
{
struct Timeval TV;
if (Gettimeofday (&AMP;TV, NULL) = = 1)
return 0.0;

Return (double) Tv.tv_usec + (double) tv.tv_sec * 1000000;
}

Static Pyobject */* Returns Object * *
Tcpping (Pyobject *self, Pyobject *args)
{
struct sockaddr_in addr;
struct Hostent *hp;
Double time;
char *host = NULL;
int FD;
int port, timeout;

if (! Pyarg_parsetuple (args, "sii", &host, &port, &timeout)) * * Convert Python-> C * * *
return NULL; /* Null=raise Exception * *

if (FD = socket (af_inet, sock_stream, 0)) < 0) {
Return Py_buildvalue ("D",-1.0); /* Convert C-> Python * *
}

Bzero ((char *) &addr, sizeof (addr));
if (HP = gethostbyname (host) = = NULL) {
Return Py_buildvalue ("D",-2.0); /* Convert C-> Python * *
}
Bcopy (Hp->h_addr, &addr.sin_addr, hp->h_length);
addr.sin_family = af_inet;
Addr.sin_port = htons (port);

struct Timeval TV;

tv.tv_sec = 0;
Tv.tv_usec = timeout * 1000;

Double stime = MyTime ();
if (Connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
Return Py_buildvalue ("D",-3.0); /* Convert C-> Python * *
}
Fd_set read, write;
Fd_zero (&read);
Fd_zero (&write);

Fd_set (FD, &read);
Fd_set (FD, &write);

if (select (FD + 1, &read, &write, NULL, &tv) = = 0) {
Close (FD);
Return Py_buildvalue ("D",-4.0); /* Convert C-> Python * *
}

Double etime = MyTime ();
Time = Etime-stime;
if (! Fd_isset (FD, &read) &&! Fd_isset (FD, &write)) {
Close (FD);
Return Py_buildvalue ("D",-4.0); /* Convert C-> Python * *
}
Close (FD);
Return Py_buildvalue ("D", time/1000); /* Convert C-> Python * *
}

/* Registration Table * *
static struct Pymethoddef portping_methods[] = {
{"Tcpping", Tcpping, Meth_varargs},/* method name, C func ptr, always-tuple * *
{NULL, NULL}/* End of table marker * *
};

/* Module initializer * *
void inittcpportping ()/* called on the
{/* Name matters if loaded dynamically * *
(void)   Py_initmodule ("tcpportping", portping_methods); /* MoD name, table ptr * *
}

Compile into Python module

Copy Code code as follows:
GCC Tcpportping.c-i/usr/include/python2.4-shared-l/usr/bin-fpic-lpython2.4-o tcpportping.so

Here is the code that Python calls the C module:

Copy Code code as follows:
#!/usr/bin/env python

Import tcpportping
Import time

i = 0
While I < 5:
t = tcpportping.tcpping (' www.baidu.com ', 80, 1000)
If T < 0:
Print "Time Out"
Else
Print T
Time.sleep (0.5)
i + 1

Executing the Python code can result in a port ping, which, in the case of a test, is almost no different from a normal ping.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.