将字符串从C发送到Python程序

将字符串从C发送到Python程序,python,c,string,connect,Python,C,String,Connect,我必须将一个用C编写的程序与另一个用Python(Linux操作系统)编写的程序连接起来。第一个应该每隔几秒钟发送一个特定的字符串,并且必须由Python程序接收才能执行某些任务 有什么建议或例子吗 问候 Maxi这是一个C程序(send_string.C),它输出字符串“Hello”: 编译C程序后,您可以使用以下方式连接Linux中的程序: 如果要重复执行此操作,例如每5秒,可以使用while和sleep命令: while true; do ./send_string | python re

我必须将一个用C编写的程序与另一个用Python(Linux操作系统)编写的程序连接起来。第一个应该每隔几秒钟发送一个特定的字符串,并且必须由Python程序接收才能执行某些任务

有什么建议或例子吗

问候 Maxi

这是一个C程序(
send_string.C
),它输出字符串“Hello”:

编译C程序后,您可以使用以下方式连接Linux中的程序:

如果要重复执行此操作,例如每5秒,可以使用
while
sleep
命令:

while true; do ./send_string | python receive_string.py; sleep 5; done

要停止循环,请按Ctrl+C。如果您知道要执行循环多少次,可以使用
来代替
循环。

我在这里找到并修改了

它使用客户机-服务器结构,使用库。 Zeromq以任何语言在任何平台上连接您的代码,在inproc、IPC、TCP、TIPC、多播和其他方面传输消息

这里是服务器:

/*  original Time Server - Modified: zeromq_1.c
Author - Samitha Ransara
www.mycola.info

Comp:  gcc -Wall -g zeromq_1.c -lzmq -o zeromq_1
*/


#include <zmq.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>


int main (void)
{
    printf ("Sockets initializing\r\n");
    //  Socket to talk to clients
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://127.0.0.1:5555");
    assert (rc == 0);

    char tmpbuf[128];
    char buffer [10];
    int i=0;


    while(i<6)
    {

        snprintf(tmpbuf, sizeof(tmpbuf), "Mensaje Nro %i\n",i);
        zmq_recv (responder, buffer, 10, 0);
        printf ("Request Recieved\r\n");

        //zmq_send (responder, tmpbuf, 8, 0);
        zmq_send (responder, tmpbuf, 16, 0);
        printf ("Responded with %s\r\n",tmpbuf);
        i++;
    }
    return 0;
}

这个问题很模糊,很难理解,通常令人困惑。你能提供一个答案吗。这是什么操作系统?
./send_string | python receive_string.py
while true; do ./send_string | python receive_string.py; sleep 5; done
/*  original Time Server - Modified: zeromq_1.c
Author - Samitha Ransara
www.mycola.info

Comp:  gcc -Wall -g zeromq_1.c -lzmq -o zeromq_1
*/


#include <zmq.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>


int main (void)
{
    printf ("Sockets initializing\r\n");
    //  Socket to talk to clients
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://127.0.0.1:5555");
    assert (rc == 0);

    char tmpbuf[128];
    char buffer [10];
    int i=0;


    while(i<6)
    {

        snprintf(tmpbuf, sizeof(tmpbuf), "Mensaje Nro %i\n",i);
        zmq_recv (responder, buffer, 10, 0);
        printf ("Request Recieved\r\n");

        //zmq_send (responder, tmpbuf, 8, 0);
        zmq_send (responder, tmpbuf, 16, 0);
        printf ("Responded with %s\r\n",tmpbuf);
        i++;
    }
    return 0;
}
#  PYzeromq_1.py
#  

from __future__ import print_function
import zmq,time,sys



def main():

    print("Connecting to Data Service…")
    sys.stdout.flush()
    context = zmq.Context()

    #  Socket to talk to server
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://127.0.0.1:5555")
    print("Connected....")

    while(True):

        #print("\r\nSending request …")
        socket.send("Requesting... ")

        #  Get the reply.
        message = socket.recv()

        #print("Time %s" % message, end='\r')
        print(message)
        time.sleep(1)

    return 0

if __name__ == '__main__':
    main()