Python中socket经ssl加密后server开多线程
前几天手撸Python socket代码,撸完之后经过ssl加密,确保数据的安全,外加server端开启多线程保证一个客户端连接有一个线程来服务客户端,走了不少的弯路,网上的信息啥的要么有ssl没有服务端的多线程,要不只有多线程没有加ssl加密,对于新手做这种需求还是有些困难的,这里,经过我!李帅帅的实践得出各种版本的代码以及最终终结版的代码,话说妇联4都他娘的药终结了,还有几天就上映了,说票挺贵的,这他娘的看个屁,大不了过半个月在啃,不知道黑寡妇最后咋样了,啧啧,挺好的一个姑娘,那脸,那腰,那身材,那充满对李帅帅爱意恒生的眼神,咳咳,不扯了不扯了,直接上代码自己看
1.没有ssl加密没有线程前的server端
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
server.py import ssl import threading class ListenServer(object): PORT = 8000 # 监听的端口 # IP = "127.0.0.1" IP = "127.0.0.1" def __init__(self): # 初始化 # 生成SSL上下文 # 切记!!!!!加密一定要在跟客户端连接前进行加密,否则没有意义!!!!!! self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # 加载服务器所用证书和私钥 self.context.load_cert_chain('cert/server.crt', 'cert/server.key') # 开启socket, try: # 套接字:ipv4 TCP协议 self.SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定ip以及端口号 self.SOCK.bind((self.IP, self.PORT)) print("监听iP: %s; 监听Port: %s" % (self.IP, self.PORT)) # 监听数,也可以理解服务端一次性处理多少的客户端的连接请求 self.SOCK.listen(100) except Exception as e: print("socket errer!: %s" % e) else: print("success SOCKET!") def run_pro(self, act): ''' 程序的主要函数 处理连接客户端 ''' print("server running...\r\n") # act = active.Active() with self.context.wrap_socket(self.SOCK, server_side=True) as ssock: while True: client_socket, client_ip = ssock.accept() # 获取客户端 # p = Process(target=process, args=( # client_socket, client_ip)) # 开始新进程 p = threading.Thread(target=act,args=(client_socket, client_ip)) p.start() # p.join() #线程会等待 if __name__ == "__main__": s = ListenServer() s.run_pro()
没有ssl加密没有线程前的client端
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket import ssl class client_ssl: def send_hello(self,): # 生成SSL上下文 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # 加载信任根证书 context.load_verify_locations('cert/ca.crt') print("现在证书啥的都加载完成了") # 与服务端建立socket连接 with socket.create_connection(('127.0.0.1', 8000)) as sock: print("准备连接啦") # 将socket打包成SSL socket # 一定要注意的是这里的server_hostname不是指服务端IP,而是指服务端证书中设置的CN,我这里正好设置成127.0.1而已 with context.wrap_socket(sock, server_hostname='127.0.0.1') as ssock: # 向服务端发送信息 print("向服务端发送信息") msg = "do i connect with server ?".encode("utf-8") ssock.send(msg) # 接收服务端返回的信息 msg = ssock.recv(1024).decode("utf-8") print("receive msg from server :" , msg) ssock.close() if __name__ == "__main__": client = client_ssl() client.send_hello()
2.没有ssl加密,只在服务端开启多线程的server
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from socketserver import BaseRequestHandler,ThreadingTCPServer import threading class Hander(BaseRequestHandler): def handle(self): ''' 实现并发的效果就是重写父类的handle方法(直接写逻辑,连接准备listen()等都干好了) :return: ''' address = self.client_address print(address, "客户端连接了!!") while 1: # 接受客户端的数据 data = self.request.recv(1024) # 判断连接与否 if len(data) > 0: print("客户端", address, data.decode("utf-8")) cur_thread = threading.current_thread() self.request.sendall('response'.encode("utf-8")) else: print("关闭连接") break if __name__ == '__main__': HOST = '192.168.0.177' PORT = 8000 ADDR = (HOST, PORT) server = ThreadingTCPServer(ADDR,Hander) print("listening") server.serve_forever()
3.没有加线程的server端
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!C:\Python3.6.5\python.exe # -*- coding: gbk -*- import socket import ssl import threading class WSGIServer(object): def __init__(self, port): """初始化对象""" # 生成SSL上下文 self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # 指定ssl版本 # 加载信任根证书 self.context.load_cert_chain(certfile="cert/ca.crt", keyfile="cert/ca.key") # self.context.load_verify_locations('cert.pem') # server端的证书 # self.context.load_verify_locations('key.pem') # server端的 print("现在证书啥的都加载完成了") # 创建套接字 print(self.context) self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 解决程序端口占用问题 self.tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 绑定本地ip地址 self.tcp_server_socket.bind(("127.0.0.1", port)) # 将套接字变为监听套接字,最大连接数量为100 self.tcp_server_socket.listen(100) print("最大连接数是100昂") def run_forever(self): """设备连接""" print("等待设备的链接ing。。。") # 1.等待设备连接(通过ip地址和端口建立tcp连接) # 如果有设备连接,则会生成用于设备和服务器通讯的套接字:new_socket # 会获取到设备的ip地址和端口 print("等待生产新的套接字。。。") print("哎呀,等到了!!!") with self.context.wrap_socket(self.tcp_server_socket, server_side=True) as ssock: # print("这一步有错") while 1: new_socket, client_addr = ssock.accept() print("设备{0}已连接".format(client_addr)) def service_machine(self, new_socket, client_addr): """业务处理""" while 1: # 3.接收设备发送的数据,单次最大1024字节,按‘gbk’格式解码 receive_data = new_socket.recv(1024).decode("gbk") # 4.如果设备发送的数据不为空 if receive_data: # 4.1 打印接收的数据,这里可以将设备发送的数据写入到文件中 # 获取设备的ID信息 print(receive_data) if receive_data[0:6] == "report": response = "SET OK:" + receive_data else: receive_data = receive_data[6:].split(",")[0] # 拼接响应数据 response = "alarm=" + receive_data + ",Switch:clear" print(response) # 4.2 返回原数据作为应答,按‘utf-8’格式编码 new_socket.send(response.encode("utf-8")) # 5.当设备断开连接时,会收到空的字节数据,判断设备已断开连接 else: print('设备{0}断开连接...'.format(client_addr)) break # 关闭套接字 new_socket.close() def main(port): """创建一个WEB服务器""" wsgi_server = WSGIServer(port) print("服务器已开启") wsgi_server.run_forever() if __name__ == '__main__': port = 8000 # 指定端口 main(port)
4.ssl+多线程的server端!!!!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!C:\Python3.6.5\python.exe # -*- coding: gbk -*- import socket import ssl import threading class WSGIServer(object): def __init__(self, port): """初始化对象""" # 生成SSL上下文 self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # 指定ssl版本 # 加载信任根证书 self.context.load_cert_chain(certfile="cert/ca.crt", keyfile="cert/ca.key") # self.context.load_verify_locations('cert.pem') # server端的证书 # self.context.load_verify_locations('key.pem') # server端的 print("现在证书啥的都加载完成了") # 创建套接字 print(self.context) self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 解决程序端口占用问题 self.tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 绑定本地ip地址 self.tcp_server_socket.bind(("127.0.0.1", port)) # 将套接字变为监听套接字,最大连接数量为100 self.tcp_server_socket.listen(100) print("最大连接数是100昂") def run_forever(self): """设备连接""" print("等待设备的链接ing。。。") # 1.等待设备连接(通过ip地址和端口建立tcp连接) # 如果有设备连接,则会生成用于设备和服务器通讯的套接字:new_socket # 会获取到设备的ip地址和端口 print("等待生产新的套接字。。。") print("哎呀,等到了!!!") with self.context.wrap_socket(self.tcp_server_socket, server_side=True) as ssock: # print("这一步有错") while 1: new_socket, client_addr = ssock.accept() print("设备{0}已连接".format(client_addr)) # # 2.创建线程处理设备的需求 t1 = threading.Thread(target=self.service_machine, args=(new_socket, client_addr)) t1.start() def service_machine(self, new_socket, client_addr): """业务处理""" while 1: # 3.接收设备发送的数据,单次最大1024字节,按‘gbk’格式解码 receive_data = new_socket.recv(1024).decode("gbk") # 4.如果设备发送的数据不为空 if receive_data: # 4.1 打印接收的数据,这里可以将设备发送的数据写入到文件中 # 获取设备的ID信息 print(receive_data) if receive_data[0:6] == "report": response = "SET OK:" + receive_data else: receive_data = receive_data[6:].split(",")[0] # 拼接响应数据 response = "alarm=" + receive_data + ",Switch:clear" print(response) # 4.2 返回原数据作为应答,按‘utf-8’格式编码 new_socket.send(response.encode("utf-8")) # 5.当设备断开连接时,会收到空的字节数据,判断设备已断开连接 else: print('设备{0}断开连接...'.format(client_addr)) break # 关闭套接字 new_socket.close() def main(port): """创建一个WEB服务器""" wsgi_server = WSGIServer(port) print("服务器已开启") wsgi_server.run_forever() if __name__ == '__main__': port = 8000 # 指定端口 main(port)