我正在尝试从python代码中调用C函数,该C函数如下:
#include <stdio.h>
#include <stddef.h>
#include "picohttpparser.c"
#include "picohttpparser.h"
const char *path;
char buf[4096];
int pret, minor_version, i;
struct phr_header headers[100];
size_t buflen = 0, prevbuflen = 0, method_len, path_len, num_headers;
ssize_t rret;
void parse_http_request(char *request, const char *method) {
/* parse the request */
strncpy(buf, request, 4096);
num_headers = sizeof(headers) / sizeof(headers[0]);
phr_parse_request(buf, sizeof(buf) -1, &method, &method_len, &path, &path_len, &minor_version, headers, &num_headers, 0);
}
在python中,我正在使用ctypes:
import ctypes
_cparser = ctypes.CDLL('/tmp/serv.so')
_cparser.parse_http_request.argtypes = (ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p))
def parse(request):
method = ctypes.c_char_p()
_cparser.parse_http_request(ctypes.c_char_p(request.encode()), ctypes.byref(method))
print(method)
方法始终为无,但是如果我编译C代码,它将正确打印出方法变量。我在这里想念什么?