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 (&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.