使用python請求介面(可進行併發測試)

2021-10-21 02:36:27 字數 1108 閱讀 9023

python可以支援多個執行緒,所以可以利用python對寫好的介面進行併發測試。請求介面**如下:

#coding=utf-8

import requests

import json

import threading

import time

import uuid

class postrequests():

def __init__(self):

self.url = 'https://***.*****.com/user/user/login' #請求鏈結

self.data = #傳遞引數

self.data = json.dumps(self.data)

def post(self):

try:

r = requests.post(self.url, self.data, headers=self.headers)

print(r.text)

except exception as e:

print(e)

def kquan_bf():

login = postrequests()

return login.post()

try:

i = 0

# 開啟執行緒數目

tasks_number = 1

print('測試啟動')

time1 = time.perf_counter()

while i < tasks_number:

t = threading.thread(target=kquan_bf)

t.start()

i +=1

time2 = time.perf_counter()

times = time2 - time1

print(times/tasks_number) #每次請求用時

except exception as e:

print(e) #請求結果

結果顯示:

測試啟動

0.0005290000000000017

>>>

封裝使用axios進行介面請求

npm install axios在main.js引入 import axios from axios 引入 import axios from axios import from vant toust是vant元件中輕提示,可根據專案更改設定環境切換axios baseurl if process...

Python使用指定埠進行http請求的例子

使用requests庫 s.get https 程式設計客棧 baidu.com 我用wireshark測試發現是走的54321埠。使用pycurl庫 c pycurl.curl c.setopt c.url,c.setopt c.localport,54321 c.setopt c.localpo...

使用Ktor進行HTTP請求

ktor 這是乙個 kotlin 官方打造的 web 框架,除了提供 http 服務外,ktor 還為和客戶端提供了配套的庫,用來進行 http 請求。當然我們已經有retrofit等其他庫完成類似功能,但是作為kotlin親兒子,ktor與coroutine的搭配更加天衣無縫。接下來我們通過乙個小...