• 浏览器配套


    from http.server import BaseHTTPRequestHandler, HTTPServer
    import logging
    from urllib.parse import urlparse
    
    # 保存结果
    filename = r'res1.txt'
    
    """
    
    var data = new FormData();
    
    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;
    
    xhr.addEventListener("readystatechange", function() {
      if(this.readyState === 4) {
        console.log(this.responseText);
      }
    });
    
    xhr.open("GET", "http://127.0.0.1:8081/https://1.1.1.1/xietansheng/article/details/115559160");
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Pragma", "no-cache");
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("no-cache", "1");
    
    xhr.send(data);
    """
    
    
    class S(BaseHTTPRequestHandler):
        def _set_response(self):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
    
        def do_GET(self):
            logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
            self._set_response()
            if self.path == '/res1.txt':
                fin = open("res1.txt")
                content = fin.read()
                fin.close()
                self.wfile.write("{}".format(str(content)).encode('utf-8'))
                return
            if self.path == '/':
                self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
            else:
                self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
    
                # 写入数据
                with open(filename, 'a+') as f:
                    f.write(urlparse(self.path[1:]).netloc + '\n')
    
    
    
    def do_POST(self):
        content_length = int(self.headers['Content-Length'])  # <--- Gets the size of data
        post_data = self.rfile.read(content_length)  # <--- Gets the data itself
        logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
                     str(self.path), str(self.headers), post_data.decode('utf-8'))
    
        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
    
    
    def run(server_class=HTTPServer, handler_class=S, port=8080):
        logging.basicConfig(level=logging.INFO)
        server_address = ('', port)
        httpd = server_class(server_address, handler_class)
        logging.info('Starting httpd...\n')
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        logging.info('Stopping httpd...\n')
    
    
    if __name__ == '__main__':
        run(port=int(8081))
    
  • 相关阅读:
    Centos下安装Redis
    BZOJ 4870 [Shoi2017]组合数问题 ——动态规划 矩阵乘法
    BZOJ 4868 [Shoi2017]期末考试 ——三分 枚举
    BZOJ 4584 [Apio2016]赛艇 ——动态规划
    BZOJ 2806 [Ctsc2012]Cheat ——后缀自动机 单调队列优化DP
    BZOJ 2330 [SCOI2011]糖果 ——差分约束系统 SPFA
    Topcoder SRMCards ——贪心
    CTSC 1999 家园 【网络流24题】星际转移
    BZOJ 3489 A simple rmq problem ——KD-Tree
    BZOJ 2733 [HNOI2012]永无乡 ——线段树 并查集
  • 原文地址:https://www.cnblogs.com/17bdw/p/15511395.html
Copyright © 2020-2023  润新知