menu
more_vert
http 请求头回显

作者:dayu

代码速记:

#encoding:utf-8
#author:walker
#date:2017-07-24
#summary:回显http头,可用于匿名度检测

importargparse
fromcoloredimportfg,bg,attr
fromhttp.serverimportHTTPServer,BaseHTTPRequestHandler

classEchoHTTPHandler(BaseHTTPRequestHandler):
#将请求头包装成html,便于返回给http客户端
deftext_to_html(self,req_head):
html=‘<html><head><title>EchoHTTPHeader</title></head>‘
html+=‘<body><div>‘
html+=‘<fontcolor="blue">%s-%s-%s</font><br/><br/>‘%(self.client_address,self.request_version,self.path)
forlineinreq_head.split(‘\n‘):
line=line.strip()
ifline.startswith(‘Via:‘)orline.startswith(‘X-Forwarded-For:‘):
line=‘<fontcolor="red">%s</font><br/>‘%line
else:
line=‘<fontcolor="black">%s</font><br/>‘%line
html+=line
html+=‘</div></body></html>‘

returnhtml

#响应get请求,打印http头,并返回给http客户端
defdo_GET(self):
print(‘%s-%s-%s‘%(self.client_address,self.request_version,self.path))
print(type(self.client_address))
print(‘###requestheaders###‘)
req_head=str(self.headers)
forlineinreq_head.split(‘\n‘):
line=line.strip()
ifline.startswith(‘Via:‘)orline.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=‘EchoHTTPserver.‘)
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.addressor‘0.0.0.0‘,args.portor8080),EchoHTTPHandler)
server.serve_forever()


相关阅读:

1、http.server — HTTP servers

2、colored


*** walker ***


本文出自 “walker的流水账” 博客,请务必保留此出处http://walkerqt.blog.51cto.com/1310630/1950505

http 请求头回显

原文地址:http://walkerqt.blog.51cto.com/1310630/1950505