代码速记:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#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,略有改动。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/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()
|
相关阅读:
2、colored
*** walker ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1950505如需转载请自行联系原作者
RQSLT