您好,登錄后才能下訂單哦!
代碼速記:
#encoding: utf-8 #author: walker #date: 2017-07-24 #summary: 回顯http頭,可用于匿名度檢測 #sys.version: '3.5.2 (default, Nov 17 2016, 17:05:23) \n[GCC 5.4.0 20160609]' import argparse from colored import fg, bg, attr from http.server import HTTPServer, BaseHTTPRequestHandler class EchoHTTPHandler(BaseHTTPRequestHandler): #將請求頭包裝成html,便于返回給http客戶端 def text_to_html(self, req_head): html = '<html><head><title>Echo HTTP Header</title></head>' html += '<body><div>' html += '<font color="blue">%s - %s - %s</font><br/><br/>' % (self.client_address, self.request_version, self.path) for line in req_head.split('\n'): line = line.strip() if line.startswith('Via:') or line.startswith('X-Forwarded-For:'): line = '<font color="red">%s</font><br/>' % line else: line = '<font color="black">%s</font><br/>' % line html += line html += '</div></body></html>' return html #響應get請求,打印http頭,并返回給http客戶端 def do_GET(self): print('%s - %s - %s' % (self.client_address, self.request_version, self.path)) print(type(self.client_address)) print('### request headers ###') req_head = str(self.headers) for line in req_head.split('\n'): line = line.strip() if line.startswith('Via:') or line.startswith('X-Forwarded-For:'): line = '%s%s%s' % (fg('red'), line, attr('reset')) print(line) self.send_response(200) self.end_headers() self.wfile.write(self.text_to_html(req_head).encode('utf8')) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Echo HTTP server.') parser.add_argument('-a', '--address', help='default: 0.0.0.0') parser.add_argument('-p', '--port', help='default: 8080', type=int) args = parser.parse_args() server = HTTPServer((args.address or '0.0.0.0', args.port or 8080), EchoHTTPHandler) server.serve_forever()
【http json 回顯】
以下代碼 copy 自:https://gist.github.com/bsingr/a5ef6834524e82270154a9a72950c842,略有改動。
#!/usr/bin/env python3 from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import urlparse import json class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): parsed_path = urlparse(self.path) self.send_response(200) self.end_headers() self.wfile.write(json.dumps({ 'method': self.command, 'path': self.path, 'real_path': parsed_path.query, 'query': parsed_path.query, 'request_version': self.request_version, 'protocol_version': self.protocol_version }).encode()) return def do_POST(self): #content_len = int(self.headers.getheader('content-length')) content_len = int(self.headers['Content-Length']) post_body = self.rfile.read(content_len) data = json.loads(post_body) parsed_path = urlparse(self.path) self.send_response(200) self.end_headers() self.wfile.write(json.dumps({ 'method': self.command, 'path': self.path, 'real_path': parsed_path.query, 'query': parsed_path.query, 'request_version': self.request_version, 'protocol_version': self.protocol_version, 'body': data }).encode()) return if __name__ == '__main__': server = HTTPServer(('localhost', 8000), RequestHandler) print('Starting server at http://localhost:8000') server.serve_forever()
相關閱讀:
1、http.server — HTTP servers
2、colored
*** walker ***
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。