首页 > 代码库 > python异步socket编程之一
python异步socket编程之一
异步网络能极大地提高程序的并行处理能力,所以写了一个专题来总结python中的异步通信。
#!/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=http://www.mamicode.com/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 = http://www.mamicode.com/raw_input(‘>‘)
if not data:
break
tcpCliSock.send(data)
data=http://www.mamicode.com/tcpCliSock.recv(BUFSIZE)
if not data:
break
print data
tcpCliSock.close()
一个服务端,一个客户端,而且是阻塞方式,一次只能接受一个客户端连接并通信噢。
客户端发送‘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 = http://www.mamicode.com/tcpCliSock.recv(BUFSIZE)
data = http://www.mamicode.com/data.decode(‘utf8‘)
if not data:
break
ss = ‘[%s] %s‘ %(ctime(), data)
tcpCliSock.send(ss.encode(‘utf8‘))
print(ss)
if data =http://www.mamicode.com/= ‘bye‘:
quit = True
break
elif data =http://www.mamicode.com/= ‘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 = http://www.mamicode.com/input(‘>‘)
if not data:
continue
print(‘input data: [%s]‘ %data)
tcpCliSock.send(data.encode(‘utf8‘))
rdata = http://www.mamicode.com/tcpCliSock.recv(BUFSIZE)
if not rdata:
break
print(rdata.decode(‘utf8‘))
if data == ‘bye‘ or data =http://www.mamicode.com/= ‘shutdown‘:
break
tcpCliSock.close()
#!/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 = http://www.mamicode.com/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
一、同步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=http://www.mamicode.com/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 = http://www.mamicode.com/raw_input(‘>‘)
if not data:
break
tcpCliSock.send(data)
data=http://www.mamicode.com/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 = http://www.mamicode.com/tcpCliSock.recv(BUFSIZE)
data = http://www.mamicode.com/data.decode(‘utf8‘)
if not data:
break
ss = ‘[%s] %s‘ %(ctime(), data)
tcpCliSock.send(ss.encode(‘utf8‘))
print(ss)
if data =http://www.mamicode.com/= ‘bye‘:
quit = True
break
elif data =http://www.mamicode.com/= ‘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 = http://www.mamicode.com/input(‘>‘)
if not data:
continue
print(‘input data: [%s]‘ %data)
tcpCliSock.send(data.encode(‘utf8‘))
rdata = http://www.mamicode.com/tcpCliSock.recv(BUFSIZE)
if not rdata:
break
print(rdata.decode(‘utf8‘))
if data == ‘bye‘ or data =http://www.mamicode.com/= ‘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 = http://www.mamicode.com/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编程之一
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。