We know that python is a scripting language with many advantages such as high development speed, but there are some differences in execution efficiency. How can we take into account the development speed and execution efficiency.
In fact, Python can be easily extended using the C language. Let's take a look at it.
Here we implement a python extension that uses libcurl to request a webpage.
First, we need Python. h to directly install python-devel on RHEL.
Create a file named httpmodule. c.
After opening, write the following code:
#include <Python.h><stdio.h><curl/curl.h>
Then write the execution function
PyObject **self,PyObject **== (res != Py_BuildValue(
This function has a fixed pattern
PyObject **self,PyObject *args)
Then write the export Function
PyMethodDef httpMethods[]=,http_get,METH_VARARGS,
Then the module initialization Function
*=Py_InitModule(
Then we can compile it.
Run the following command:
Gcc-fpic-c-I/usr/include/python2.6/httpmodule. c
Gcc -- shared-o http. so httpmodule. o-lcurl
Then we enter the python interaction mode.
>>> Import http
>>> Dir (http)
['_ Doc _', '_ file _', '_ name _', '_ package _', 'get']
>>> Http. get ()
<! DOCTYPE html> <! -- Status OK -->
So far, we have successfully compiled the python C extension.