{"id":49990,"date":"2020-03-12T09:53:04","date_gmt":"2020-03-12T01:53:04","guid":{"rendered":"http:\/\/4563.org\/?p=49990"},"modified":"2020-03-12T09:53:04","modified_gmt":"2020-03-12T01:53:04","slug":"%e8%af%b7%e6%95%99python%e4%bb%a3%e7%a0%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=49990","title":{"rendered":"\u8bf7\u6559python\u4ee3\u7801\u95ee\u9898"},"content":{"rendered":"\n<p>  \t\t\t\t\t<strong>\u5927\u5e05\u9505<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3>\u8bf7\u6559python\u4ee3\u7801\u95ee\u9898<\/h3>\n<p>  \t\t#!\/usr\/bin\/env python<br \/>  # coding:utf-8<br \/>  import sys<br \/>  import BaseHTTPServer<br \/>  import httplib<br \/>  import SocketServer<br \/>  import struct<br \/>  import socket<br \/>  import select<br \/>  import threading<br \/>  import logging<br \/>  import urlparse<\/p>\n<p>  import flashGet<\/p>\n<p>  BIND_IP = &#8216;0.0.0.0&#8217;<br \/>  BIND_PORT = 27015# I love Counter-Strike<\/p>\n<p>  # flashGet.debuglevel = 1<br \/>  logging._srcfile = None<br \/>  logging.logProcesses = 0<br \/>  logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(asctime)s &#8211; %(message)s&#8217;, datefmt=&#8217;%H:%M:%S&#8217;)<\/p>\n<p>  urlparse.MAX_CACHE_SIZE = 100<\/p>\n<p>  class ProxyServer(SocketServer.ThreadingTCPServer):<br \/>  &nbsp; &nbsp; allow_reuse_address = True<br \/>  &nbsp; &nbsp; daemon_threads = True<\/p>\n<p>  class ProxyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler, object):<br \/>  &nbsp; &nbsp; # Set this to HTTP\/1.1 to enable automatic keepalive<br \/>  &nbsp; &nbsp; protocol_version = &#8216;HTTP\/1.1&#8217;<br \/>  &nbsp; &nbsp; <br \/>  &nbsp; &nbsp; def parse_request(self):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if not super(ProxyRequestHandler, self).parse_request():<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return False<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;pconntype = self.headers.get(&#8216;Proxy-Connection&#8217;, &#8221;)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if &#8216;close&#8217; in pconntype.lower():# some say closed<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.close_connection = 1<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;elif (pconntype.lower() == &#8216;keep-alive&#8217; and<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.protocol_version &gt;= &#8216;HTTP\/1.1&#8217;):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.close_connection = 0<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;return True<\/p>\n<p>  &nbsp; &nbsp; def handle_one_request(self):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;self.stopped = threading.Event() #TODO: need refine<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;self.raw_requestline = self.rfile.readline()<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if not self.raw_requestline:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.close_connection = 1<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if not self.parse_request():<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; # An error code has been sent, just exit<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;if self.get_scheme() != &#8216;http&#8217;:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.send_error(501, &#8216;%s is not supported, only http works.&#8217; % self.get_scheme())<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;# self.headers = BaseHTTPRequestHandler.headers = instance of mimetools.Message<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;fg = flashGet.FlashGet(<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; method=self.command, <br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; url=self.path, <br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; payload=self.client_request_body(), <br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; headers=self.headers.dict,<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; stopped = self.stopped<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;for data in fg.download():<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.wfile.write(data)<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;self.wfile.flush()# actually send the response if not already done.<\/p>\n<p>  &nbsp; &nbsp; def handle(self):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&quot;&quot;&quot;Handle multiple requests if necessary.&quot;&quot;&quot;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;self.close_connection = 1<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;try:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.handle_one_request()<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; # while not self.close_connection:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #&nbsp; &nbsp;&nbsp; &nbsp;self.handle_one_request()<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;except socket.error as e:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.stopped.set()<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.send_error(500, str(e))<\/p>\n<p>  &nbsp; &nbsp; def client_request_body(self):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;content_length = int(self.headers.get(&#8216;Content-Length&#8217;, 0))<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;return self.rfile.read(content_length) if content_length else None<\/p>\n<p>  &nbsp; &nbsp; def get_scheme(self):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;url_obj = urlparse.urlparse(self.path, scheme=&#8217;grass-mud-horse&#8217;)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if url_obj.scheme == &#8216;grass-mud-horse&#8217;:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.path = &#8216;http:\/\/&#8217;+self.path<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return &#8216;http&#8217;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;return url_obj.scheme<\/p>\n<p>  &nbsp; &nbsp; def send_error(self, code, message):<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;#TODO: use logging for thread-safe<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;super(ProxyRequestHandler, self).send_error(code, message)<\/p>\n<p>  def main():<br \/>  &nbsp; &nbsp; if sys.argv:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;port = int(sys.argv)<br \/>  &nbsp; &nbsp; else:<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;port = BIND_PORT<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br \/>  &nbsp; &nbsp; server_address = (BIND_IP, port)<br \/>  &nbsp; &nbsp; httpd = ProxyServer(server_address, ProxyRequestHandler)<\/p>\n<p>  &nbsp; &nbsp; sa = httpd.server_address<br \/>  &nbsp; &nbsp; print &quot;Serving proxy on&quot;, sa, &quot;port&quot;, sa, &quot;&#8230;&quot;<br \/>  &nbsp; &nbsp; httpd.serve_forever()<\/p>\n<p>  if __name__ == &#8216;__main__&#8217;:<br \/>  &nbsp; &nbsp; main()<\/p>\n<p>  \u8fd9\u6bb5\u4ee3\u7801\u5982\u4f55\u4fee\u6539\u8ba9\u5176\u652f\u6301https\u7684\u8bbf\u95ee\uff1f\u76ee\u524d\u53ea\u80fd\u8bbf\u95eehttp\t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>usbcdrom<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\tBUZHIDAO\t\t\t  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5e05\u9505 \u5927\u4f6c\u6709\u8bdd\u8bf4 : \u8bf7\u6559pyt&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/49990"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49990"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/49990\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49990"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}