当前位置: 欧洲杯竞猜 > 计算机知识 > 正文

python进程间通讯,Python编写的socket服务器和顾客

时间:2019-08-22 17:36来源:计算机知识

服务器端:

欧洲杯竞猜,python实现进程间通信简单实例

python异步socket编程之一

异步网络能极大地提高程序的并行处理能力,所以写了一个专题来总结python中的异步通信。

#!/usr/bin/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

一、同步client与同步server的通信

import socket

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!n" % data
connection.send("hello,client")
connection.close()

1.1. 《python的socket通信实例》中的例子

  1. TCP server端代码
    #!/usr/bin/env python
    #
    # -*- coding:utf-8 -*-
    # File: sync_socket_server.py
    #
    from socket import *
    from time import ctime

HOST = ''
PORT = 21567
BUFSIZE=1024
ADDR=(HOST, PORT)

tcpSrvSock=socket(AF_INET, SOCK_STREAM)
tcpSrvSock.bind(ADDR)
tcpSrvSock.listen(5)

while True:
print 'waiting for connection ...'
tcpCliSock,addr = tcpSrvSock.accept()
print '... connected from:', addr

while True:
data=tcpCliSock.recv(BUFSIZE)
if not data:
break
tcpCliSock.send('[%s] %s'%(ctime(), data))
print [ctime()],':',data

tcpCliSock.close()
tcpSrvSock.close()

  1. TCP client端代码
    #!/usr/bin/env python
    #
    # -*- coding:utf-8 -*-
    # File: sync_socket_client.py

from socket import *

HOST='localhost'
PORT=21567
BUFSIZE=1024
ADDR=(HOST, PORT)

tcpCliSock=socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
data = raw_input('>')
if not data:
break
tcpCliSock.send(data)
data=tcpCliSock.recv(BUFSIZE)
if not data:
break
print data

tcpCliSock.close()

host='127.0.0.1'

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

2.2. 另一个例子

这个例子的链接:
一个服务端,一个客户端,而且是阻塞方式,一次只能接受一个客户端连接并通信噢。
客户端发送‘bye', 结束与服务端的通信,如果发送’shutdown',服务端将会关闭自己!

  1. 服务端代码:
    #!/usr/bin/env python
    #
    # -*- coding:utf-8 -*-
    # File: sync_socket_server.py
    #
    from socket import *
    from time import ctime

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
quit = False
shutdown = False

while True:
print('waiting for connection...')
tcpCliSock, addr = tcpSerSock.accept()
print('...connected from: ', addr)

while True:
data = tcpCliSock.recv(BUFSIZE)
data = data.decode('utf8')
if not data:
break
ss = '[%s] %s' %(ctime(), data)
tcpCliSock.send(ss.encode('utf8'))
print(ss)
if data == 'bye':
quit = True
break
elif data == 'shutdown':
shutdown = True
break
print('Bye-bye: [%s: %d]' %(addr[0], addr[1]))
tcpCliSock.close()
if shutdown:
break
tcpSerSock.close()
print('Server has been')

  1. 客户端代码
    #!/usr/bin/env python
    #
    # -*- coding:utf-8 -*-
    # File: sync_socket_client.py
    #
    from socket import *
    HOST = 'localhost'
    PORT = 21567
    BUFSIZE = 1024
    ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
data = input('>')
if not data:
continue
print('input data: [%s]'

编辑:计算机知识 本文来源:python进程间通讯,Python编写的socket服务器和顾客

关键词: 欧洲杯竞猜