网络编程补充与进程,线程与全局解释器锁

asyncio模块是python之父写的模块,按说应该是可信赖的,python3.6版本定义为平安版本。

python多线程socket编制程序之多客商端连着,pythonsocket

Python中贯彻socket通讯的服务端相比复杂,而客商端特别轻便,所以顾客端基本上都以用sockct模块实现,而服务
端用有繁多模块能够运用,如下:

图片 1

1、客户端

#!/usr/bin/env python
#coding:utf-8
'''
file:client.py
date:9/9/17 3:43 PM
author:lockey
email:[email protected]
desc:socket编程客户端,python3.6.2
'''
import socket,sys
HOST = '192.168.1.6'
PORT = 8998
ADDR =(HOST,PORT)
BUFSIZE = 1024

sock = socket.socket()
try:
 sock.connect(ADDR)
 print('have connected with server')

 while True:
  data = input('lockey# ')
  if len(data)>0:
  print('send:',data)
  sock.sendall(data.encode('utf-8')) #不要用send()
  recv_data = sock.recv(BUFSIZE)
  print('receive:',recv_data.decode('utf-8'))
  else:
  sock.close()
  break
except Exception:
 print('error')
 sock.close()
 sys.exit()

2、SocketServer模块

为了能够让四个客商端同一时间对接服务并开展通讯,服务端将应用SocketServer模块来贯彻,那样的话客户不用在子线程甘休后,利用父进度对它举办拍卖,也不用关爱socket的倒闭,一切都由SocketServer来成功。

#!/usr/bin/env python
#coding:utf-8
'''
file:client.py
date:9/9/17 3:43 PM
author:lockey
email:[email protected]
desc:socket编程服务器端,python3.6.2
'''
from socketserver import BaseRequestHandler,ThreadingTCPServer
import threading

BUF_SIZE=1024

class Handler(BaseRequestHandler):
 def handle(self):
  address,pid = self.client_address
  print('%s connected!'
			

本文由星彩网app下载发布于星彩网app下载,转载请注明出处:网络编程补充与进程,线程与全局解释器锁

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。