码迷,mamicode.com
首页 > 编程语言 > 详细

python异步socket编程之一

时间:2014-12-09 19:46:02      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   使用   sp   for   

异步网络能极大地提高程序的并行处理能力,所以写了一个专题来总结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

python异步socket编程之一

标签:style   blog   http   io   ar   os   使用   sp   for   

原文地址:http://blog.csdn.net/fireroll/article/details/41826803

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!