Python full stack Development--socketserver module and verify the legitimacy of client links

Source: Internet
Author: User
Tags hmac

Verifying the legitimacy of client links

Implementation of a simple client link authentication function in a distributed system

#_*_coding:utf-8_*_ fromSocketImport*ImportHmac,ossecret_key=b'Linhaifeng Bang Bang Bang'defConn_auth (conn):" "Authentication Client Link:P Aram Conn:: Return:" "    Print('start verifying the legitimacy of new links') msg=os.urandom (32) Conn.sendall (msg) H=hmac.new (secret_key,msg) Digest=h.digest () respone=Conn.recv (Len (Digest))returnhmac.compare_digest (respone,digest)defData_handler (conn,bufsize=1024):    if  notConn_auth (conn):Print('the link is illegal, close') Conn.close ()return    Print('link Legal, start communication')     whileTrue:data=conn.recv (bufsize)if  notData BreakConn.sendall (Data.upper ())defServer_handler (ip_port,bufsize,backlog=5):    " "Process only links:p Aram Ip_port:: Return:" "Tcp_socket_server=socket (af_inet,sock_stream) tcp_socket_server.bind (Ip_port) Tcp_socket_server.listen (backlog) whiletrue:conn,addr=tcp_socket_server.accept ()Print('new Connection [%s:%s]'% (addr[0],addr[1])) Data_handler (conn,bufsize)if __name__=='__main__': Ip_port=('127.0.0.1', 9999) BufSize=1024Server_handler (ip_port,bufsize) service side
Service Side
#_*_coding:utf-8_*___author__='Linhaifeng' fromSocketImport*ImportHmac,ossecret_key=b'Linhaifeng Bang Bang Bang'defConn_auth (conn):" "Verify that the client-to-server link:P Aram Conn:: Return:" "msg=CONN.RECV (32) H=hmac.new (secret_key,msg) Digest=h.digest () Conn.sendall (Digest)defClient_handler (ip_port,bufsize=1024): Tcp_socket_client=socket (af_inet,sock_stream) tcp_socket_client.connect (ip_port) Conn_auth (tcp_socket_client) whileTrue:data=input ('>>:'). Strip ()if  notDataContinue        ifdata = ='quit': BreakTcp_socket_client.sendall (Data.encode ('Utf-8')) Respone=tcp_socket_client.recv (bufsize)Print(Respone.decode ('Utf-8') ) Tcp_socket_client.close ()if __name__=='__main__': Ip_port=('127.0.0.1', 9999) BufSize=1024Client_handler (ip_port,bufsize) client (legal)
Client
#_*_coding:utf-8_*___author__='Linhaifeng' fromSocketImport*defClient_handler (ip_port,bufsize=1024): Tcp_socket_client=socket (af_inet,sock_stream) tcp_socket_client.connect (ip_port) whileTrue:data=input ('>>:'). Strip ()if  notDataContinue        ifdata = ='quit': BreakTcp_socket_client.sendall (Data.encode ('Utf-8')) Respone=tcp_socket_client.recv (bufsize)Print(Respone.decode ('Utf-8') ) Tcp_socket_client.close ()if __name__=='__main__': Ip_port=('127.0.0.1', 9999) BufSize=1024Client_handler (ip_port,bufsize) client (illegal: Do not know encryption method)
Client (illegal: Do not know encryption method)
#_*_coding:utf-8_*___author__='Linhaifeng' fromSocketImport*ImportHmac,ossecret_key=b'Linhaifeng Bang Bang bang1111'defConn_auth (conn):" "Verify that the client-to-server link:P Aram Conn:: Return:" "msg=CONN.RECV (32) H=hmac.new (secret_key,msg) Digest=h.digest () Conn.sendall (Digest)defClient_handler (ip_port,bufsize=1024): Tcp_socket_client=socket (af_inet,sock_stream) tcp_socket_client.connect (ip_port) Conn_auth (tcp_socket_client) whileTrue:data=input ('>>:'). Strip ()if  notDataContinue        ifdata = ='quit': BreakTcp_socket_client.sendall (Data.encode ('Utf-8')) Respone=tcp_socket_client.recv (bufsize)Print(Respone.decode ('Utf-8') ) Tcp_socket_client.close ()if __name__=='__main__': Ip_port=('127.0.0.1', 9999) BufSize=1024Client_handler (ip_port,bufsize) client (illegal: do not know Secret_key)
Client (illegal: do not know Secret_key)Socketserver Module

Implementation of a one-to-many interaction mode between server and client based on TCP protocol

ImportSocketserverclassMyServer (socketserver. Baserequesthandler):defHandle (self):#Self.request is the equivalent of a conn         whileTrue:Print(self.client_address) msg= Self.request.recv (1024x768). Decode ('Utf-8')            ifmsg = ='Q': Break            Print(msg) Info= Input ('%s>>>'%msg[:2]) self.request.send (Info.encode ('Utf-8'))if __name__=='__main__': Server= Socketserver. Threadingtcpserver (('127.0.0.1', 8080), MyServer)#Thread ThreadsServer.allow_reuse_address =True server.serve_forever ()
Service Side
ImportSocketsk=socket.socket () Sk.connect ('127.0.0.1', 8080)) whiletrue:msg= Input ('>>>')    ifmsg = ='Q': sk.send (b'Q')         BreakSk.send (('American Regiment:'+msg). Encode ('Utf-8')) RET= Sk.recv (1024x768). Decode ('Utf-8')    Print(ret) sk.close ()
Client

Python full stack Development--socketserver module and verify the legitimacy of client links

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.