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.