Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7888667
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: Python/Ruby

2014-12-09 17:46:07

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

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

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()

2. 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()

2.2. 另一个例子

这个例子的链接:http://blog.csdn.net/joeblackzqq/article/details/34860379
一个服务端,一个客户端,而且是阻塞方式,一次只能接受一个客户端连接并通信噢。
客户端发送‘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')

2. 客户端代码
#!/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]' %data)
    tcpCliSock.send(data.encode('utf8'))
    rdata = tcpCliSock.recv(BUFSIZE)
    if not rdata:
       break
    print(rdata.decode('utf8'))
    if data == 'bye' or data == 'shutdown':
        break
tcpCliSock.close()

二、同步client与异步server的通信

1. 服务端代码
#!/usr/bin/env python
#
# -*- coding:utf-8 -*-
# File: async_socket_server.py
#

import socket
import threading
import SocketServer

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.current_thread()
        response = "{}: {}".format(cur_thread.name, data)
        self.request.sendall(response)

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 0

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.daemon = True
    server_thread.start()
    print "Server loop running in thread:", server_thread.name

    server.serve_forever()
    #server.shutdown()

2. 客户端代码
#!/usr/bin/env python
#
# -*- coding:utf-8 -*-
# File: sync_socket_client.py
#
import socket
import threading
import SocketServer

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        sock.sendall(message)
        response = sock.recv(1024)
        print "Received: {}".format(response)
    finally:
        sock.close()

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 0

    client(ip, port, "Hello World 1")
    client(ip, port, "Hello World 2")
    client(ip, port, "Hello World 3")

3. 多线程客户端代码
为了提高并行度,可以使用多线程方式调用
#!/usr/bin/env python
#
# -*- coding:utf-8 -*-
# File: sync_socket_client.py
#
import socket
import threading
import SocketServer

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        sock.sendall(message)
        response = sock.recv(1024)
        print "Received: {}".format(response)
    finally:
        sock.close()

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 0

    th1 = threading.Thread(target=client, args=(ip, port, "Hello World 1",))
    th2 = threading.Thread(target=client, args=(ip, port, "Hello World 2",))
    th3 = threading.Thread(target=client, args=(ip, port, "Hello World 3",))
    th1.start()
    th2.start()
    th3.start()
   
    th1.join()
    th2.join()
    th3.join()

4. 输出示例
Received: Thread-2: Hello World 1
Received: Thread-3: Hello World 2
Received: Thread-4: Hello World 3
阅读(5979) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~