json模塊可能須要下載,詳細方法請百度,增長我的的能力json
主要是json的,接收到非json的http請求,返回"415, "Only json data is supported."app
from http.server import BaseHTTPRequestHandler,HTTPServer import cgi import json def Cjson(datas): try: messages = json.loads(datas) except ValueError: return False return True class TodoHandler(BaseHTTPRequestHandler): """A simple TODO server which can display and manage todos for you. """ # Global instance to store todos. You should use a database in reality. TODOS = [] def do_GET(self): # return all todos if self.path != '/': self.send_error(404, "File not found.") return # Just dump data to json, and return it message = json.dumps(self.TODOS) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(bytes(message,'utf-8')) def do_POST(self): """Add a new todo Only json data is supported, otherwise send a 415 response back. Append new todo to class variable, and it will be displayed in following get request """ ctype, pdict = cgi.parse_header(self.headers['content-type']) if ctype == 'application/json': length = int(self.headers['content-length']) dd = self.rfile.read(length) post_values =Cjson(str(dd,'UTF-8')) if post_values == False: print(dd) self.TODOS.append(str(dd, 'UTF-8')) self.send_response(200) self.send_header(b'Content-type', b'application/json') self.end_headers() jss = { "code": 200, "msg": "OK", } r = json.dumps(jss) self.wfile.write(bytes(r, 'utf-8')) if post_values == True: self.TODOS.append(json.loads(str(dd,'utf-8'))) self.send_response(200) self.send_header(b'Content-type', b'application/json') self.end_headers() jss = { "code": 200, "msg": "OK", } r = json.dumps(jss) self.wfile.write(bytes(r, 'utf-8')) else: length = int(self.headers['content-length']) post_values = self.rfile.read(length) self.send_error(415, "Only json data is supported.") return if __name__ == '__main__': # Start a simple server, and loop forever server = HTTPServer(('localhost', 8888), TodoHandler) print("Starting server, use <Ctrl-C> to stop") server.serve_forever()
1.在本地運行,注意不要將線程設置太大,機器抗不住,只能重啓電腦了oop
2.主要用戶簡單的實戰,看看本身的成果post
運行上面的代碼線程
建立線程組code
建立http 請求頭server
建立聚合報告blog
查看結果樹utf-8
建立彙總報告get
開始執行
疑問解答QQ羣:羣1:588402570,羣2 772588688
羣1 限制人數後,請申請羣2
關注該公衆號:持續更新Jmeter相關內容