{"id":3484,"date":"2022-03-06T21:12:42","date_gmt":"2022-03-06T13:12:42","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=3484"},"modified":"2024-05-05T14:19:16","modified_gmt":"2024-05-05T06:19:16","slug":"python-%e4%b8%ad%e8%b0%83%e7%94%a8-c-%e8%af%ad%e8%a8%80","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=3484","title":{"rendered":"Python \u4e2d\u8c03\u7528 C \u8bed\u8a00"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=3484\/#%E5%89%8D%E8%A8%80\" >\u524d\u8a00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=3484\/#%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\" >\u6d4b\u8bd5\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=3484\/#Python_%E8%B0%83%E7%94%A8%E5%8A%A8%E6%80%81%E5%BA%93\" >Python \u8c03\u7528\u52a8\u6001\u5e93<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=3484\/#%E7%BC%96%E5%86%99_Python_%E6%89%A9%E5%B1%95\" >\u7f16\u5199 Python \u6269\u5c55<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>\u53d1\u5e03\u4e8e <a href=\"http:\/\/blog.iyatt.com\" target=\"_blank\" rel=\"noreferrer noopener\">blog.iyatt.com<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%89%8D%E8%A8%80\"><\/span>\u524d\u8a00<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python \u4e3b\u8981\u4f18\u52bf\u5e94\u8be5\u662f\u5728\u4e8e\u8f6e\u5b50\u6bd4\u8f83\u591a\uff0c\u5b89\u88c5\u4e5f\u65b9\u4fbf\uff0c\u4f9d\u8d56\u4e8e\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5f00\u53d1\u4e00\u4e2a\u4e1c\u897f\u6709\u65f6\u5019\u4f1a\u5341\u5206\u7b80\u5355\u3002C \u8bed\u8a00\u7684\u7684\u8bdd\uff0c\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5feb\uff0c\u4ee3\u7801\u7f16\u8bd1\u6210\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u76f4\u63a5\u4ea4\u7ed9 CPU \u6267\u884c\uff0c\u800c\u4e0d\u50cf Python \u90a3\u6837\u7531\u89e3\u91ca\u5668\u6267\u884c\u3002\u6709\u65f6\u5019\u53ef\u80fd\u4e5f\u9700\u8981\u5404\u81ea\u727a\u7272\u4e00\u90e8\u5206\u4f18\u52bf\uff0c\u4e24\u8005\u8fdb\u884c\u7ed3\u5408\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982 Python \u4e2d\u5e38\u7528\u7684 NumPy \u5e93\uff0c\u5b83\u7684\u5e95\u5c42\u5176\u5b9e\u4e5f\u662f C \u8bed\u8a00\u3002<\/p>\n\n\n\n<p>\u6765\u81ea\u4e8e GitHub \u5bf9 NumPy \u4ee3\u7801\u7ec4\u6210\u6bd4\u4f8b\u7edf\u8ba1\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"358\" height=\"160\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-35.png\" alt=\"\" class=\"wp-image-3485 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-35.png 358w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-35-300x134.png 300w\" data-sizes=\"(max-width: 358px) 100vw, 358px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 358px; --smush-placeholder-aspect-ratio: 358\/160;\" \/><\/figure>\n\n\n\n<p>Python \u57fa\u4e8e\u89e3\u91ca\u5668\u6267\u884c\uff0c\u6240\u4ee5\u8de8\u5e73\u53f0\u6027\u4f1a\u6bd4\u8f83\u597d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00\u4e2a\u4eba\u53ea\u4f1a\u6c49\u8bed\uff0c\u6709\u4e00\u5929\u4ed6\u524d\u5f80\u65e5\u672c\uff0c\u8eab\u8fb9\u5c31\u7ed9\u4ed6\u5b89\u6392\u4e86\u4e00\u4e2a\u65e2\u61c2\u65e5\u8bed\u53c8\u61c2\u6c49\u8bed\u7684\u4eba\u505a\u7ffb\u8bd1\uff0c\u90a3\u4e48\u4ed6\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u7ffb\u8bd1\u548c\u5176\u5b83\u65e5\u672c\u4eba\u4ea4\u6d41\uff1b\u67d0\u4e00\u5929\u4ed6\u53c8\u524d\u5f80\u4e86\u7f8e\u56fd\uff0c\u8eab\u8fb9\u5c31\u5e26\u4e86\u4e00\u4e2a\u61c2\u82f1\u8bed\u548c\u6c49\u8bed\u7684\u7ffb\u8bd1\uff0c\u90a3\u4e48\u4ed6\u4e5f\u80fd\u901a\u8fc7\u7ffb\u8bd1\u548c\u7f8e\u56fd\u4eba\u4ea4\u6d41\u3002Python \u5c31\u76f8\u5f53\u4e8e\u90a3\u4e2a\u53ea\u4f1a\u6c49\u8bed\u7684\u4eba\uff0c\u800c Python \u89e3\u91ca\u5668\u5c31\u76f8\u5f53\u4e8e\u8fd9\u4e2a\u662f\u7ffb\u8bd1\uff0c\u5728\u4e0d\u540c\u5e73\u53f0\u5c31\u5bf9\u5e94\u5728\u4e0d\u540c\u56fd\u5bb6\uff0c\u4f46\u662f\u6709\u76f8\u5e94\u7684\u89e3\u91ca\u5668\uff0c\u4e5f\u5c31\u76f8\u5f53\u4e8e\u8fd9\u4e2a\u4eba\u5728\u4e0d\u540c\u56fd\u5bb6\u6709\u5bf9\u5e94\u8bed\u8a00\u7684\u7ffb\u8bd1\u3002<\/p>\n\n\n\n<p>C \u8bed\u8a00\u7684\u8bdd\uff0c\u5199\u7684\u4ee3\u7801\u662f\u8981\u76f4\u63a5\u7f16\u8bd1\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4e0d\u540c\u5e73\u53f0\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0d\u901a\u7528\uff0c\u5f53\u7136\u5982\u679c\u5199\u7684\u4ee3\u7801\u53ea\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u7684\u8bdd\uff0c\u5c06\u4ee3\u7801\u653e\u5230\u4e0d\u540c\u5e73\u53f0\u4e0b\u7684\u7f16\u8bd1\u5668\u5e94\u8be5\u662f\u53ef\u4ee5\u7f16\u8bd1\u51fa\u5bf9\u5e94\u5e73\u53f0\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u4f46\u662f\u5982\u679c\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u5e93\u7684\u8bdd\uff0c\u90a3\u5c31\u4e0d\u4e00\u5b9a\u4e86\uff0c\u8981\u770b\u5bf9\u5e94\u5e73\u53f0\u4e0b\u6709\u6ca1\u6709\u76f8\u5e94\u7684\u5e93\u3002\u5728 Windows \u4e0b\u53ef\u6267\u884c\u7a0b\u5e8f\u6269\u5c55\u540d\u4e3a .exe\uff0c\u9759\u6001\u5e93 .lib\uff0c\u52a8\u6001\u5e93 .dll\uff1bLinux \u4e0b\u53ef\u6267\u884c\u7a0b\u5e8f\u6ca1\u6709\u6307\u5b9a\u6269\u5c55\u540d\uff0c\u53ea\u662f\u6587\u4ef6\u5c5e\u6027\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u9759\u6001\u5e93 .a\uff0c\u52a8\u6001\u5e93 .so\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\"><\/span>\u6d4b\u8bd5\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ubuntu 20.04 x86_64<\/p>\n\n\n\n<p>Python 3.9.10<\/p>\n\n\n\n<p>GNU gcc 9.4.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_%E8%B0%83%E7%94%A8%E5%8A%A8%E6%80%81%E5%BA%93\"><\/span>Python \u8c03\u7528\u52a8\u6001\u5e93<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6587\u6863\uff1a<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/ctypes.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.python.org\/zh-cn\/3\/library\/ctypes.html<\/a><\/p>\n\n\n\n<p>C \u5e93\u4ee3\u7801\uff0c\u547d\u540d\u4e3a module.c<\/p>\n\n\n\n<p>\u4e3b\u8981\u662f\u6f14\u793a\u51e0\u79cd\u5e38\u7528\u6570\u636e\u7c7b\u578b\u5728 Python \u548c C \u4e2d\u7684\u5bf9\u5e94\u5173\u7cfb<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int f_int(int i)\n{\n    return i;\n}\n\n\nfloat f_float(float f)\n{\n    return f;\n}\n\n\ndouble f_double(double d)\n{\n    return d;\n}\n\n\nchar f_char(char f)\n{\n    return f;\n}\n\n\nchar *f_str(char *s)\n{\n    return s;\n}\n\nint add(int num1, int num2)\n{\n    return num1 + num2;\n}<\/pre>\n\n\n\n<p>\u7f16\u8bd1\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">gcc module.c -o libmodule.so -fPIC -shared -Wall -O3 -std=c17<\/pre>\n\n\n\n<p>Python \u4e3b\u7a0b\u5e8f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from ctypes import c_int, c_float, c_double, c_char, c_char_p\nfrom ctypes import cdll\n\n\nmodule = cdll.LoadLibrary('libmodule.so')  # \u5bfc\u5165\u52a8\u6001\u5e93\n\nf_int = module.f_int  # \u5f15\u7528\u52a8\u6001\u5e93\u4e2d\u7684\u51fd\u6570\nf_int.argtypes = [c_int]  # \u51fd\u6570\u53c2\u6570\u7c7b\u578b\nf_int.restype = c_int  # \u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\nprint(f_int(1))\n\nf_float = module.f_float\nf_float.argtypes = [c_float]\nf_float.restype = c_float\nprint(f_float(1.0))\n\nf_double = module.f_double\nf_double.argtypes = [c_double]\nf_double.restype = c_double\nprint(f_double(1.0))\n\nf_char = module.f_char\nf_char.argtypes = [c_char]\nf_char.restype = c_char\nprint(f_char(b'a').decode('utf-8'))\n\nf_str = module.f_str\nf_str.restype = c_char_p\nstr = c_char_p('abcd1234'.encode('utf-8'))\nprint(f_str(str).decode('utf-8'))\n\nf_add = module.add\nf_add.argtypes = [c_int, c_int]\nf_add.restype = c_int\nnum1 = 1\nnum2 = 9\nprint(f_add(num1, num2))<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"533\" height=\"223\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-37.png\" alt=\"\" class=\"wp-image-3503 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-37.png 533w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-37-300x126.png 300w\" data-sizes=\"(max-width: 533px) 100vw, 533px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 533px; --smush-placeholder-aspect-ratio: 533\/223;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%BC%96%E5%86%99_Python_%E6%89%A9%E5%B1%95\"><\/span>\u7f16\u5199 Python \u6269\u5c55<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6587\u6863\uff1a<a href=\"https:\/\/docs.python.org\/zh-cn\/3.9\/extending\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.python.org\/zh-cn\/3.9\/extending\/index.html<\/a><\/p>\n\n\n\n<p>\u793a\u4f8b\u5c31\u7528 C \u8bed\u8a00\u5c01\u88c5\u4e00\u4e2a system() \u51fd\u6570\u4f9b\u7ed9 Python \u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u4e00\u4e2a\u6a21\u5757\u4e3a spam\uff0c\u542b\u6709\u4e00\u4e2a\u65b9\u6cd5\u4e3a system<\/p>\n\n\n\n<p>\u2460 \u5934\u6587\u4ef6\u5f15\u7528<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u5934\u6587\u4ef6\u5df2\u7ecf\u5305\u542b\u5e38\u7528\u7684 C \u6807\u51c6\u5e93<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define PY_SSIZE_T_CLEAN\n#include \"Python.h\"<\/pre>\n\n\n\n<p>\u2461 \u51fd\u6570\u5c01\u88c5<\/p>\n\n\n\n<p>\u5373\u63d0\u4f9b\u7ed9 Python \u8c03\u7528\u7684\u65b9\u6cd5<\/p>\n\n\n\n<p>\u83b7\u53d6\u81ea Python \u4f20\u5165\u7684\u53c2\u6570\uff0c\u4ea4\u7531 C \u5e93\u51fd\u6570\u6267\u884c\uff0c\u5e76\u5c06\u8fd4\u56de\u503c\u4f20\u56de Python<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">static PyObject *spam_system(PyObject *self, PyObject *args)\n{\n    const char *command;\n    int sts;\n\n    if (!PyArg_ParseTuple(args, \"s\", &amp;command))  \/\/ \u53c2\u6570\u89e3\u6790\n    {\n        return NULL;\n    }\n    sts = system(command);\n    return PyLong_FromLong(sts);\n}<\/pre>\n\n\n\n<p>\u2462 \u5b9a\u4e49\u65b9\u6cd5\u8868<\/p>\n\n\n\n<p>\u544a\u77e5 Python \u8fd9\u4e2a\u6a21\u5757\u542b\u6709\u4ec0\u4e48\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">static PyMethodDef SpamMethods[] =\n{\n    {\"system\",  spam_system, METH_VARARGS, \"\u901a\u8fc7\u8be5\u65b9\u6cd5\u53ef\u6267\u884c\u7ec8\u7aef\u547d\u4ee4\"},\n    {NULL, NULL, 0, NULL}  \/\/ \u7ed3\u5c3e\n};<\/pre>\n\n\n\n<p>\u2463 \u6a21\u5757\u5b9a\u4e49\u7ed3\u6784<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u6a21\u5757\u7ed3\u6784\u4e2d\u5f15\u7528\u524d\u9762\u5b9a\u4e49\u7684\u65b9\u6cd5\u8868<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">static struct PyModuleDef spammodule =\n{\n    PyModuleDef_HEAD_INIT,\n    \"spam\",  \/\/ \u6a21\u5757\u540d\n    NULL,  \/\/ \u6a21\u5757\u6587\u6863\n    -1,\n    SpamMethods  \/\/ \u65b9\u6cd5\u8868\n};<\/pre>\n\n\n\n<p>\u2464 \u521d\u59cb\u5316\u6a21\u5757<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PyMODINIT_FUNC PyInit_spam(void)\n{\n    return PyModule_Create(&amp;spammodule);\n}<\/pre>\n\n\n\n<p>\u2465 \u6d4b\u8bd5<\/p>\n\n\n\n<p>\u4ee3\u7801\u5408\u5e76\uff0c\u547d\u540d\u4e3a spam.c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define PY_SSIZE_T_CLEAN\n#include \"Python.h\"\n\n\nstatic PyObject *spam_system(PyObject *self, PyObject *args)\n{\n    const char *command;\n    int sts;\n\n    if (!PyArg_ParseTuple(args, \"s\", &amp;command))\n    {\n        return NULL;\n    }\n    sts = system(command);\n    return PyLong_FromLong(sts);\n}\n\n\nstatic PyMethodDef SpamMethods[] =\n{\n    {\"system\",  spam_system, METH_VARARGS, \"\u901a\u8fc7\u8be5\u65b9\u6cd5\u53ef\u6267\u884c\u7ec8\u7aef\u547d\u4ee4\"},\n    {NULL, NULL, 0, NULL}  \/\/ \u7ed3\u5c3e\n};\n\n\nstatic struct PyModuleDef spammodule =\n{\n    PyModuleDef_HEAD_INIT,\n    \"spam\",  \/\/ \u6a21\u5757\u540d\n    NULL,  \/\/ \u6a21\u5757\u6587\u6863\n    -1,\n    SpamMethods  \/\/ \u65b9\u6cd5\u8868\n};\n\n\nPyMODINIT_FUNC PyInit_spam(void)\n{\n    return PyModule_Create(&amp;spammodule);\n}<\/pre>\n\n\n\n<p>\u540c\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a setup.py<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from distutils.core import setup, Extension\n\nmodule1 = Extension('spam',\n                     sources=['spam.c'],\n                     extra_compile_args=['-std=c17'])\n\nsetup (name='spam',\n       version='0.1',\n       description='spam \u6a21\u5757',\n       author='IYATT-yx',\n       author_email='iyatt@iyatt.com',\n       license='AGPL-3.0',\n       url='iyatt.com',\n       platforms='Ubuntu 20.04 x86_64',\n       ext_modules=[module1])<\/pre>\n\n\n\n<p>\u5b89\u88c5\u6a21\u5757<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m pip install .<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"441\" height=\"54\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-36.png\" alt=\"\" class=\"wp-image-3498 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-36.png 441w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/image-36-300x37.png 300w\" data-sizes=\"(max-width: 441px) 100vw, 441px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 441px; --smush-placeholder-aspect-ratio: 441\/54;\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import spam\n\n\nspam.system('ls -l')<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"558\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe-1024x558.png\" alt=\"\" class=\"wp-image-3499 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe-1024x558.png 1024w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe-300x163.png 300w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe-768x418.png 768w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe-1536x836.png 1536w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-55-56-\u7684\u5c4f\u5e55\u622a\u56fe.png 1629w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/558;\" \/><\/figure>\n\n\n\n<p>\u2466 \u6a21\u5757\u53d1\u5e03<\/p>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u76f4\u63a5\u5b89\u88c5\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u5c06\u6a21\u5757\u53d1\u5e03\u51fa\u53bb\uff0c\u7528\u6237\u4f7f\u7528 pip \u5c31\u80fd\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m pip install [\u6a21\u5757\u6587\u4ef6\u540d]<\/pre>\n\n\n\n<p>[1] \u53d1\u5e03\u6e90\u7801\u5305<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 setup.py sdist<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"894\" height=\"461\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-59-56-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-3500 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-59-56-\u7684\u5c4f\u5e55\u622a\u56fe.png 894w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-59-56-\u7684\u5c4f\u5e55\u622a\u56fe-300x155.png 300w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-15-59-56-\u7684\u5c4f\u5e55\u622a\u56fe-768x396.png 768w\" data-sizes=\"(max-width: 894px) 100vw, 894px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 894px; --smush-placeholder-aspect-ratio: 894\/461;\" \/><\/figure>\n\n\n\n<p>[2] \u53d1\u5e03\u4e8c\u8fdb\u5236\u5305<\/p>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m pip install whell<\/pre>\n\n\n\n<p>\u6784\u5efa\u4e8c\u8fdb\u5236\u5305<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip3 wheel --wheel-dir=bin .<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"894\" height=\"371\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-16-02-50-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-3501 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-16-02-50-\u7684\u5c4f\u5e55\u622a\u56fe.png 894w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-16-02-50-\u7684\u5c4f\u5e55\u622a\u56fe-300x124.png 300w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/03\/2022-03-06-16-02-50-\u7684\u5c4f\u5e55\u622a\u56fe-768x319.png 768w\" data-sizes=\"(max-width: 894px) 100vw, 894px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 894px; --smush-placeholder-aspect-ratio: 894\/371;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u53d1\u5e03\u4e8e blog.iyatt.com \u524d\u8a00 Python \u4e3b\u8981\u4f18\u52bf\u5e94\u8be5\u662f\u5728\u4e8e\u8f6e\u5b50\u6bd4\u8f83\u591a\uff0c\u5b89\u88c5\u4e5f\u65b9\u4fbf\uff0c\u4f9d\u8d56\u4e8e\u4e30\u5bcc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-3484","post","type-post","status-publish","format-standard","hentry","category-all"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/3484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3484"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/3484\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}