python:验证客户端链接的合法性与socketserver

2018-06-18 01:00:12来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

一、验证客户端链接的合法性

 1 from socket import *
 2 import hmac,os
 3 
 4 secret_key=b'linhaifeng bang bang bang'
 5 def conn_auth(conn):
 6     '''
 7     认证客户端链接
 8     :param conn:
 9     :return:
10     '''
11     print('开始验证新链接的合法性')
12     msg=os.urandom(32)
13     conn.sendall(msg)
14     h=hmac.new(secret_key,msg)
15     digest=h.digest()
16     respone=conn.recv(len(digest))
17     return hmac.compare_digest(respone,digest)
18 
19 def data_handler(conn,bufsize=1024):
20     if not conn_auth(conn):
21         print('该链接不合法,关闭')
22         conn.close()
23         return
24     print('链接合法,开始通信')
25     while True:
26         data=conn.recv(bufsize)
27         if not data:break
28         conn.sendall(data.upper())
29 
30 def server_handler(ip_port,bufsize,backlog=5):
31     '''
32     只处理链接
33     :param ip_port:
34     :return:
35     '''
36     tcp_socket_server=socket(AF_INET,SOCK_STREAM)
37     tcp_socket_server.bind(ip_port)
38     tcp_socket_server.listen(backlog)
39     while True:
40         conn,addr=tcp_socket_server.accept()
41         print('新连接[%s:%s]' %(addr[0],addr[1]))
42         data_handler(conn,bufsize)
43 
44 if __name__ == '__main__':
45     ip_port=('127.0.0.1',9999)
46     bufsize=1024
47     server_handler(ip_port,bufsize)
服务端
 1 __author__ = 'Linhaifeng'
 2 from socket import *
 3 import hmac,os
 4 
 5 secret_key=b'linhaifeng bang bang bang'
 6 def conn_auth(conn):
 7     '''
 8     验证客户端到服务器的链接
 9     :param conn:
10     :return:
11     '''
12     msg=conn.recv(32)
13     h=hmac.new(secret_key,msg)
14     digest=h.digest()
15     conn.sendall(digest)
16 
17 def client_handler(ip_port,bufsize=1024):
18     tcp_socket_client=socket(AF_INET,SOCK_STREAM)
19     tcp_socket_client.connect(ip_port)
20 
21     conn_auth(tcp_socket_client)
22 
23     while True:
24         data=input('>>: ').strip()
25         if not data:continue
26         if data == 'quit':break
27 
28         tcp_socket_client.sendall(data.encode('utf-8'))
29         respone=tcp_socket_client.recv(bufsize)
30         print(respone.decode('utf-8'))
31     tcp_socket_client.close()
32 
33 if __name__ == '__main__':
34     ip_port=('127.0.0.1',9999)
35     bufsize=1024
36     client_handler(ip_port,bufsize)
客户端

二、socketserver

import socketserver
class Myserver(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "127.0.0.1", 9999

    # 设置allow_reuse_address允许服务器重用地址
    socketserver.TCPServer.allow_reuse_address = True
    # 创建一个server, 将服务地址绑定到127.0.0.1:9999
    server = socketserver.TCPServer((HOST, PORT),Myserver)
    # 让server永远运行下去,除非强制停止程序
    server.serve_forever()
server端
import socket

HOST, PORT = "127.0.0.1", 9999
data = "hello"

# 创建一个socket链接,SOCK_STREAM代表使用TCP协议
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.connect((HOST, PORT))          # 链接到客户端
    sock.sendall(bytes(data + "\n", "utf-8")) # 向服务端发送数据
    received = str(sock.recv(1024), "utf-8")# 从服务端接收数据

print("Sent:     {}".format(data))
print("Received: {}".format(received))
client

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:芝麻HTTP:记scikit-learn贝叶斯文本分类的坑

下一篇:python_字符串的操作