For more information, see the code. Compile a socket-Based C/S structure program in Python. Then, both parties can simulate HTTP Communication and directly access the server through a browser.
Server :#! /Usr/bin/ENV Python # encoding: utf-8import socket, sys, threading Server = '2017. 0.0.1 '# Host IP Port = 8080 # port number maxthreads = 10 recvbuflen = 1024 httptag = 'HTTP/1. 'mytag = '^ myp 1.0' rstr = ''' HTTP/1.1 200 okproxy-connection: Keep-aliveconnection: Keep-alivecontent-length: 8296via: 1.1 JA-ISA02Expires: Fri, 18 May 2012 09:05:56 gmtdate: Fri, 18 May 2012 09:05:56 gmtcontent-type: text/html; charset = gb23 12 server: BWS/1.0cache-control: privatetest ''' rstr2 = ''' my defined p... '''rstr3 = ''' unknown p... '''userlist = ['user01', 'user02'] baduser = 'bad' class Checker (threading. thread): def _ init _ (self, socket, num): threading. thread. _ init _ (Self) self. socket = socket self. num = num print 'thread started! 'Def run (Self): While true: Self. socket. listen (2) CS, address = self. socket. accept () recvstr = cs. recv (recvbuflen) print '>>>>>>>>>>>>> thread [% d] received: \ r \ n % s' % (self. num, recvstr) If recvstr = '': Print 'empty request' CS. close () return tag = recvstr. split ('\ r \ n', 1) [0] If httptag in Tag: Print 'HTTP request' CS. send (rstr) print 'response is: \ r \ n % s' % rstr Elif mytag in Tag: Print 'my define request' CS. send (rstr2) print 'response is: \ r \ n % s' % rstr2 else: Print 'other request' CS. send (rstr3) print 'response is: \ r \ n % s' % rstr3 print '>>>>>>>>>>>>> thread [% d] end check >>>>>>>>>>>>> '% (self. num,) Cs. close () Class authserver (object): def _ init _ (Self): Self. socket = none def run (Self): Self. socket = socket. socket (socket. af_inet, socket. sock_stream) self. socket. BIND (server, Port) I = 0 chkerlist = [] While I <maxthreads: chker = Checker (self. socket, I) chker. start () I = I + 1 chkerlist. append (chker) for chker in chkerlist: chker. join () If _ name _ = '_ main _': asvr = authserver () asvr. run () print 'ending'
Client:
#! /Usr/bin/ENV Python # encoding: utf-8import socket, threading Server = '2017. 0.0.1 '# Host IP Port = 8080 # port number start =' ^ myp 1.0 'start2 =' ^ myp 1.0 'start3 =' [^ st] 'buflen = 1024 user_list = ['user01 ', 'user02'] class connector (threading. thread): def _ init _ (self, num): threading. thread. _ init _ (Self) self. sock = socket. socket (socket. af_inet, socket. sock_stream) self. num = num def run (Self): Try: Self. sock. connect (server, Port) self. sock. send (start) print '>>>> sent', start rstr = self. sock. recv (buflen) print 'Received >>>>>>> ', rstr self. sock. close () return response t socket. error, E: Print e returnif _ name _ = '_ main _': cnlist = [] I = 0 while I <10: Cn = connector (I) CN. start () cnlist. append (CN) I = I + 1 for CN in cnlist: CN. join ()
The server performs a simple analysis on the request content. if the request meets the HTTP format, the request is prompted as an HTTP request and a string in the HTTP format is returned. All threads are short connections and end with one answer, it can be said that it simulates the HTTP request method.