python异步socket编程之一 - 商讯 - 正定县枫林商贸有限公司
现在的位置: 主页 > 商讯 > 文章正文
python异步socket编程之一
作者:正定县枫林商贸有限公司 来源:www.hbflsm.com 发布时间:2017-09-03 12:06:28
python异步socket编程之一


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

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站优化 http://www.feimao666.com


  • 上一篇:谈武汉企业网站建设:品牌文化价值才是网站建设的核心
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 正定县枫林商贸有限公司 ALL RIGHTS RESERVED.
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100