Example of how Python creates a multi-thread proxy Based on ThreadingTCPServer,

Source: Internet
Author: User

Example of how Python creates a multi-thread proxy Based on ThreadingTCPServer,

This example describes how to create a multi-threaded proxy Based on ThreadingTCPServer in Python. We will share this with you for your reference. The details are as follows:

# Coding = utf8from BaseHTTPServer import BaseHTTPRequestHandlerfrom SocketServer import ThreadingTCPServerimport gzipfrom StringIO import StringIOimport logginglogging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s ', datefmt = '% a, % d % B % Y % H: % M: % s', filename = 'proxy. log', filemode = 'W') class proxyHandler (BaseHTTPRequestHandler): def do_POST (self): while True: try: path = self. path if path. split ("/") [-1] = "statistics": # Get the data submitted by post datas = gzip. gzipFile (fileobj = StringIO (self. rfile. read ())). read () self. wfile. write (datas) logging. debug (datas) print datas failed t Exception, e: logging. error (e) finally: self. finish () def do_CONNECT (self): pass def do_GET (self): passdef test (): host = '2017. 0.0.1 'port = 8888 try: server = ThreadingTCPServer (host, port), proxyHandler) print 'Welcome to the Server HTTP On % s port % d... '% (host, port) server. serve_forever () handle T KeyboardInterrupt, e: logging. error (e) # print '^ C received, shutting down Server' server. socket. close () if _ name _ = '_ main _': test ()

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.