Python and C language mixed programming example

Source: Internet
Author: User

Recently, in order to test the network speed, some business servers need to Disable icmp, so using normal ping will not be able to meet my needs, therefore, I simply wrote a tcp port-based ping program. Because c execution efficiency is good, but development efficiency is low, python is highly efficient in development, however, the execution efficiency is not as high as that of C. Because it requires large-scale use, C is used to implement the core part of the code, and this part is implemented into a python module, which calls the c module by python, paste the code below

Copy codeThe Code is 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 (& 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, "ⅱ", & 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 first import */
{/* Name matters if loaded dynamically */
(Void) Py_InitModule ("tcpportping", portping_methods);/* mod name, table ptr */
}

Compile to python Module
Copy codeThe Code is as follows: gcc tcpportping. c-I/usr/include/python2.4-shared-L/usr/bin-fpic-lpython2.4-o tcpportping. so

The following is the code for python to call the c module:
Copy codeThe Code is as follows :#! /Usr/bin/env python

Import tcpportping
Import time

I = 0
While I <5:
T = tcpportping. tcpping ('www .baidu.com ', 80,100 0)
If t <0:
Print "time out"
Else:
Print t
Time. sleep (0.5)
I + = 1
The result of port ping can be achieved by executing the python code. From the test result, the execution result of this program is almost identical to that of ordinary ping.

Related Article

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.