Python police and thieves one of the implementation of the client-server communication instance _python

Source: Internet
Author: User
Tags socket

This article describes the Python police and thieves of one of the implementation of client-server communication, sharing for everyone to reference. The specific methods are analyzed as follows:

This example comes from the ISCC 2012 crack off the fourth question
The aim is to realize a thief and communicate with police by reverse police.

In fact, is an example of RSA encryption communication, we write the client and the server to achieve the above thief and police functions.

To communicate, this time we first write out the client and service side of the network connection through Python.

The service-side code is as follows:

#!/usr/bin/env python  
import socketserver from time  
import ctime  
HOST = ' 127.0.0.1 ' 
PORT =  
ADDR = (HOST, PORT)  
class Myrequesthandler (Socketserver.baserequesthandler): 
    def handle (self): 
        print ' ... connected from ... ', self.client_address while 
        True:  
            self.request.sendall (' [%s]%s '% (CTime (), SELF.REQUEST.RECV (1024)) 
 
 
Tcpserv = Socketserver.threadingtcpserver (ADDR, Myrequesthandler)  
print ' Waiting for connection ... '  
tcpserv.serve_forever () 

The client code is as follows:

#!/usr/bin/env python  
from socket import *  
host = ' 127.0.0.1 '  
PORT =  
Bufsiz = 1024  
ADDR = (host, PORT)  
 
Tcpclisock = socket (af_inet, sock_stream)  
Tcpclisock.connect (ADDR) while  
True:  
  data = Raw_ Input (' >>>>>>>>>>>> ')  
  if not data:  
    break  
  tcpclisock.send ('%s\ r\n '% data]  
  data = TCPCLISOCK.RECV (bufsiz)  
  if not data:  
    break  
  print Data.strip ()  
# Tcpclisock.close () 

This code can refer to the Python core programming

If you report python errno 10053 errors, make sure that the client's connection code must be outside the loop
Is

Tcpclisock = socket (af_inet, sock_stream)  
tcpclisock.connect (ADDR)  

To be outside of the while true.

The next time you solve the RSA encryption problem.

I hope this article will help you with your Python programming.

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.