一、参考
BaseHTTPServer wiki
二、名词解释
三、正文
1. http 协议
1.1 server 端
使用 BaseHTTPServer
构造简单的 server 端,用于接收 http GET 请求,传输文件
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
print('--get file--', self.path)
mime_type = 'application/pdf'
file_name = self.path.split('/')[-1]
f = open(self.path)
self.send_response(200)
self.send_header('Content-type', mime_type)
self.send_header('Content-Disposition', 'attachment; filename=%s' % file_name)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
try:
server = HTTPServer(('server_a_host', server_a_port), MyHandler)
server.serve_forever()
except Exception as e:
print('error', str(e))
~
1.2 client 端
使用 requests
模块,直接构造 http GET 请求
import requests
def get_file(file_path):
print('---start get file---', file_path)
url = 'http://server_a_host:server_a_port'
url = url + file_path
res = requests.get(url=url)
print(res.status_code, res.text)
if __name__ == '__main__':
file_path = '/data/test.pdf'
get_file(file_path=file_path)
1.3 tcpdump 录制 pcap 包
在 client server 中录制接收到的 http 文件流量包
tcpdump -i network_card_a 'host server_a_host and port server_a_port' -w /tmp/http_test.pcap