脚本宝典收集整理的这篇文章主要介绍了浏览器配套,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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: %snHeaders:n%sn", 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: %snHeaders:n%snnBody:n%sn",
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))
以上是脚本宝典为你收集整理的浏览器配套全部内容,希望文章能够帮你解决浏览器配套所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。