Python: verify the legality of client connections and the socketserver and pythonsocketserver

Source: Internet
Author: User
Tags hmac

Python: verify the legality of client connections and the socketserver and pythonsocketserver
1. verify the validity of client links

1 from socket import * 2 import hmac, OS 3 4 secret_key = B 'linhaifeng bang '5 def conn_auth (conn): 6''' 7 authenticate client link 8: param conn: 9: return: 10 '''11 print ('start verifying validity of the new link ') 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 ('this link is invalid, close ') 22 conn. close () 23 return24 print ('Connection valid, start communication') 25 while True: 26 data = conn. recv (bufsize) 27 if not data: break28 conn. sendall (data. upper () 29 30 def server_handler (ip_port, bufsize, backlog = 5): 31 ''' 32 only processes links 33: param ip_port: 34: return: 35 ''' 36 tcp_socket_server = socket (AF_INET, SOCK_STREAM) 37 bytes (ip_port) 38 tcp_socket_server.listen (backlog) 39 while True: 40 conn, addr = tcp_socket_server.accept () 41 print ('new connection [% s: % s] '% (addr [0], addr [1]) 42 data_handler (conn, bufsize) 43 44 if _ name _ = '_ main _': 45 ip_port = ('2017. 0.0.1 ', 9999) 46 bufsize = 102447 server_handler (ip_port, bufsize)
Server
1 _ author _ = 'linhaifeng' 2 from socket import * 3 import hmac, OS 4 5 secret_key = B 'Linhaifeng bang '6 def conn_auth (conn ): 7''' 8. Verify the connection from the client to the server. 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 bytes (ip_port) 20 21 conn_auth (tcp_socket_client) 22 23 while True: 24 data = input ('>> :'). strip () 25 if not data: continue26 if data = 'quit': break27 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 = ('2017. 0.0.1 ', 9999) 35 bufsize = 102436 client_handler (ip_port, bufsize)
Client 2. socketserver
Import socketserverclass Myserver (socketserver. baseRequestHandler): def handle (self): self. data = self. request. recv (1, 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 # Set allow_reuse_address to allow the server to reuse the socketserver address. TCPServer. allow_reuse_address = True # create a server and bind the service address to 127.0.0.1: 9999 server = socketserver. TCPServer (HOST, PORT), Myserver) # Let the server run forever, unless the program server is forcibly stopped. serve_forever ()
Server
Import socketHOST, PORT = "127.0.0.1", 9999 data = "hello" # create a socket link. SOCK_STREAM indicates that the TCP protocol with socket is used. socket (socket. AF_INET, socket. SOCK_STREAM) as sock: sock. connect (HOST, PORT) # link to the client sock. sendall (bytes (data + "\ n", "UTF-8") # send data to the server encoded ED = str (sock. recv (1024), "UTF-8") # receives data print ("Sent :{}" from the server :{}". format (data) print ("Received :{}". format (encoded ed ))
Client

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.