9159金沙游艺场-奥门金沙游艺场-[欢迎您]

9159金沙游艺场为您呈现最新的面貌!,目前注册送体验金,欢迎您将成为影视娱乐行业新的风向标,是亚洲的优质娱乐先锋。

python异步socket编程之一【9159金沙游艺场】

日期:2020-01-04编辑作者:前端

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

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

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]' %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()

  1. 客户端代码
    #!/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")

  1. 多线程客户端代码
    为了提高并行度,可以使用多线程方式调用
    #!/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()

  1. 输出示例
    Received: Thread-2: Hello World 1
    Received: Thread-3: Hello World 2
    Received: Thread-4: Hello World 3

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

本文由9159金沙游艺场-奥门金沙游艺场-[欢迎您]发布于前端,转载请注明出处:python异步socket编程之一【9159金沙游艺场】

关键词:

CentOS开启sudo权限

CentOS开启sudo权限 用惯了Ubuntu,在实践一些限令的时候sudo,然后在CentOS上也会习贯性的去sudo,而CentOS默许没张开sud...

详细>>

js实现的动画导航菜单效果代码

js达成的卡通片导航菜单效能代码,js完结动画导航菜单 本文实例叙述了js实现的卡通导航菜单作用代码。分享给大家...

详细>>

Python静态检查工具

Python静态检查工具 Python是一门动态语言。在给python传参数的时候并没有严格的类型限制。写python程序的时候,发现错...

详细>>

js观察者模式发布9159金沙游艺场

js观察者模式发布/订阅 以前在写页面的时候,从没这么多异步操作,只有在使用'setInterval'和'XMLHttpRequest'时了解了一...

详细>>