您好,登錄后才能下訂單哦!
代碼速記:
#encoding: utf-8 #author: walker #date: 2017-07-24 #summary: 回顯http頭,可用于匿名度檢測(cè) #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): #將請(qǐng)求頭包裝成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 #響應(yīng)get請(qǐng)求,打印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,略有改動(dòng)。
#!/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()
相關(guān)閱讀:
1、http.server — HTTP servers
2、colored
*** walker ***
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。