zoukankan      html  css  js  c++  java
  • python进程间通信 实例

    python实现进程间通信简单实例

    实例讲解了python实现两个程序之间通信的方法,具体方法:
    该实例采用socket实现,与socket网络编程不一样的是socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)的第一个参数是socket.AF_UNIX
    而不是 socket.AF_INET
    例中两个python程序 s.py/c.py 要先运行s.py
    基于fedora13/python2.6测试,成功实现!
    s.py代码如下:
    #!/usr/bin/env python
    import socket
    import os

    if __name__ == '__main__':
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    conn = '/tmp/conn'
    if not os.path.exists(conn):
    os.mknod(conn)
    if os.path.exists(conn):
    os.unlink(conn)
    sock.bind(conn)
    sock.listen(5)
    while True:
    connection,address = sock.accept()
    data = connection.recv(1024)
    if data == "hello,server":
    print "the client said:%s! " % data
    connection.send("hello,client")
    connection.close()

    c.py代码如下:
    #!/usr/bin/env python
    # www.jbxue.com
    import socket
    import time

    if __name__ == '__main__':
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    conn = '/tmp/conn'
    sock.connect(conn)
    time.sleep(1)
    sock.send('hello,server')
    print sock.recv(1024)
    sock.close()

  • 相关阅读:
    做接口测试最重要的知识点
    HTTP和HTTPS区别
    UVA, 686 Goldbach's Conjecture (II)
    UVA, 543 Goldbach's Conjecture
    UVA, 580 Critical Mass
    UVA, 900 Brick Wall Patterns
    UVA, 11000 Bee
    UVA, 10079 Pizza Cutting
    C++ 向量<vector>的学习
    jenkins入门
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3864615.html
Copyright © 2011-2022 走看看