无法使用Python连接Unix域套接字:没有此文件或目录

0 投票
1 回答
4826 浏览
提问于 2025-04-17 14:50

我有几段代码是用来建立一个Unix域套接字和一个使用这个套接字的服务的。不过这里面有一些让人困惑的错误,导致失败。

创建Unix域套接字的代码是用C语言写的:

#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

int main(int ac, char *av[])
{
if (ac != 4) {
    printf("Usage: %s dummy-fd sockpath binary\n", av[0]);
    exit(-1);
    }

    char *sockpn = av[2];
    char *bin = av[3];

    int srvfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (srvfd < 0) {
    perror("socket");
    exit(-1);
    }

    fcntl(srvfd, F_SETFD, FD_CLOEXEC);

    struct stat st;
    if (stat(sockpn, &st) >= 0) {
    if (!S_ISSOCK(st.st_mode)) {
        fprintf(stderr, "socket pathname %s exists and is not a socket\n",
            sockpn);
        exit(-1);
    }

    unlink(sockpn);
    }

    struct sockaddr_un addr;
    addr.sun_family = AF_UNIX;
    snprintf(&addr.sun_path[0], sizeof(addr.sun_path), "%s", sockpn);
    if (bind(srvfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        fprintf(stderr, "WARNING: cannot bind to socket %s (%s), exiting\n",
                sockpn, strerror(errno));
    exit(-1);
    }

    // allow anyone to connect; for access control, use directory permissions
    chmod(sockpn, 0777);

    listen(srvfd, 5);
    signal(SIGCHLD, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    for (;;) {
    struct sockaddr_un client_addr;
    unsigned int addrlen = sizeof(client_addr);

    int cfd = accept(srvfd, (struct sockaddr *) &client_addr, &addrlen);
    if (cfd < 0) {
        perror("accept");
        continue;
    }

    int pid = fork();
    if (pid < 0) {
        perror("fork");
        close(cfd);
        continue;
    }

    if (pid == 0) {
        // Child process
        dup2(cfd, 0);
        dup2(cfd, 1);
        close(cfd);

        execl(bin, bin, 0);
        perror("execl");
        exit(-1);
    }

    close(cfd);
    }
}

使用这个套接字的客户端Python代码如下(unixclient.py):

def call(pn, req):
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.connect(pn)
    sock.send(req)
    sock.shutdown(socket.SHUT_DOWN)
    data = ""
    while True:
        buf = sock.recv(1024)
        if not buf:
             break
        data += buf
    sock.close()
    return data

然后我在另一个Python代码中触发这段客户端代码,通过导入这个文件并调用call函数来实现:(“/jail/zoobar/echosvc”和“/jail/zoobar/echosvc/sock”都会失败)

   resp = call("/jail/zoobar/echosvc", str1)

在这里就出现了错误,显示:

   FIle "zoobar/unixclient.py", line 8, in call
    sock.connect(pn)
   File "<string>", line 1, in connect
   error: [ERROR 2] No such file or directory

但是我发誓目录和套接字确实存在(/jail/zoobar/echosvc/sock),而且权限也正确(777),所以我无法理解这个奇怪的错误。有人知道这是怎么回事吗?

非常感谢大家抽出时间来帮忙。

1 个回答

1

看起来你试图打开的文件名不对。代码大概是这样的:

resp = call("/jail/zoobar/echosvc", str1)

但是你说“文件是存在的”:

/jail/zoobar/echosvc/sock

试着把你的调用行改成:

resp = call("/jail/zoobar/echosvc/sock", str1)

撰写回答