Python写入C ++程序的stdin

时间:2013-06-16 08:45:02

标签: c++ python subprocess stdin

我有一个使用2个C ++文件的客户端 - 服务器模型。客户端和服务器通过套接字进行通信。用户将字符串输入到客户端的stdin,然后客户端将其传递给服务器。但是,我想使用python程序模拟用户。这是我的尝试:

from subprocess import Popen, PIPE, STDOUT

host_name = raw_input("Enter host name: ")
port = raw_input("Enter port: ")

p = Popen(["./client", host_name, port], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write("DEVICE:3:MALFUNCTIONING")

这不起作用。它启动客户端进程和参数,但不会将字符串DEVICE:3:MALFUNCTIONING写入其中。有什么建议吗?

这是我的客户代码:

if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

printf("Please enter the message: ");
memset(buffer, 0, 256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
        error("ERROR writing to socket");
memset(buffer, 0, 256);
n = read(sockfd,buffer,255);
if (n < 0)
        error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);

1 个答案:

答案 0 :(得分:0)

我不确定你是如何创建你的客户端和服务器的,但是我的实现与你的模拟用户的python程序几乎相同。

Github要点:https://gist.github.com/yanhan/5791613

根据您对客户端的代码段,我有一些怀疑可能出现的问题:

  1. server_addr变量未正确设置
  2. 服务器代码中的错误
  3. 就像mata所说,如果你能从stdout / stderr和stacktraces中显示任何输出,那将会更有帮助。

    编辑:只是想添加以下链接可能有所帮助:

    为方便起见,我在这里粘贴代码。

    sim.py:

    from subprocess import Popen, PIPE
    
    def run():
        # this should work if you change to
        #   host_name = raw_input
        #   port = raw_input
        host_name = '127.0.0.1'
        port = '8124'
        p = Popen(['./client', host_name, port], stdin=PIPE, close_fds=True)
        myInput = raw_input()
        p.stdin.write(myInput)
        p.communicate()
    
    if __name__ == '__main__':
        run()
    

    client.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define BUFSZ 256
    
    int main(int argc, char *argv[])
    {
        int sockfd, ret, exitCode = 0;
        char buffer[BUFSZ];
        memset(buffer, 0, BUFSZ);
        fgets(buffer, sizeof(buffer), stdin);
        char *hostname = argv[1];
        int port = atoi(argv[2]);
        // echo out the stuff just to see
        printf("hostname = %s\n", hostname);
        printf("port = %d\n", port);
        printf("msg = %s\n", buffer);
        // creates the socket
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd == -1) {
            fprintf(stderr, "client: socket() failed\n");
            exit(1);
        }
        struct sockaddr_in sockAddr;
        memset(&sockAddr, 0, sizeof(struct sockaddr_in));
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_port = htons(port);
        sockAddr.sin_addr.s_addr = inet_addr(hostname);
        // connect to server
        ret = connect(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
        if (ret == -1) {
            fprintf(stderr, "client: connect failed\n");
            exitCode = 1;
            goto done;
        }
        // send buffer
        ssize_t charsSent = send(sockfd, buffer, strlen(buffer)+1, 0);
        printf("client: chars sent: %d\n", (int)charsSent);
        /*
        // this seems to work as well
        write(sockfd, buffer, strlen(buffer)+1);
        */
    
    done:
        close(sockfd);
        return exitCode;
    }
    

    server.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define PORTNUM 8124
    #define BUFSZ 256
    
    int main(int argc, char *argv[])
    {
        char buf[BUFSZ];
        int ret, exitCode = 1;
        // create socket
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        int recvFd;
        if (sockfd == -1) {
            fprintf(stderr, "server: Error creating socket\n");
            exit(1);
        }
        struct sockaddr_in sockAddr;
        memset(&sockAddr, 0, sizeof(struct sockaddr_in));
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_port = htons(PORTNUM);
        sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        // bind socket
        ret = bind(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
        if (ret == -1) {
            fprintf(stderr, "server: bind failed\n");
            exitCode = 1;
            goto done;
        }
        // listen for connection. max of 1 connection
        ret = listen(sockfd, 1);
        if (ret == -1) {
            fprintf(stderr, "server: listen failed\n");
            exitCode = 1;
            goto done;
        }
        struct sockaddr_in dest;
        socklen_t sockLen;
        ssize_t bytesReceived;
        while (1) {
            // accept connection from client
            recvFd = accept(sockfd, (struct sockaddr *)&dest, &sockLen);
            if (recvFd == -1) {
                fprintf(stderr, "server: accept failed\n");
                exitCode = 1;
                break;
            }
            // receive message from client
            bytesReceived = recv(recvFd, buf, BUFSZ, 0);
            if (bytesReceived == -1) {
                fprintf(stderr, "server: recv failed\n");
                exitCode = 1;
                break;
            } else {
                printf("server: received %s", buf);
                fflush(stdout);
            }
        }
    done:
        close(sockfd);
        return exitCode;
    }