携带签名发送表单
python主程序
发送表单
import requests import json url = "http://10.65.6.102/api/environment/add" headers = { # 'content-type': "application/json", # 'Connection': 'close', 'signature': '0JqTXFv7QGqA3RrSkADruUmT85k+4DauRSSVYaAh4iKzs4u//2Ttst4GZ0mbb3lmx6LrxDfokMZcqL4vnB/l9iIQFKCuls7PvbNrgo4Ch1A=', 'accessKey': 'Jzx7UCA0q7pr1u5l' } #signature是一个随机值 # value_json=json.dumps({"id":"97ae2f14-82bc-4288-9a3d-99cfc1c7aba5","name":"tes111","projectId":"d01b064d-9d61-4ecc-a15a-74f8c5e03420","createUser":"sunlinlin","config":"{\"commonConfig\":{\"requestTimeout\":60000,\"responseTimeout\":60000},\"httpConfig\":{\"isMock\":false,\"conditions\":[{\"protocol\":\"http\",\"port\":\"8100\",\"domain\":\"172.16.64.98\",\"socket\":\"172.16.64.98:8100\",\"time\":1632722704875,\"type\":\"NONE\"}]}}"}) #将字典转成json字符串,注,这个字典的config值是一个json字符串,所以用有斜杠将所有引号转义 files = {'request':('name=request;content-disposition=form-data',value_json, 'application/json')} # print(type(value_json)) # print(value_json) # print('\n') # print(type(files)) # print(files) r = requests.post(url,headers=headers,files=files) print(r) print(r.text) #下面是发送普通表单,后续再整理一下 # import requests # import json # import os # payload = {"param_1": "value_1", "param_2": "value_2"} # filepath = '/file/path/to/local_file.zip' # data = {'json': ('some-json', json.dumps(payload), 'application/json'), # 'file': (os.path.basename(filepath), open(filepath, 'rb'), 'application/octet-stream')} # response = requests.post('http://127.0.0.1:80/', files = data)
signature 生成方法:
bat 脚本执行java代码
javac -cp commons-codec-1.15.jar; MeterSphereSignatureTest.java java -cp commons-codec-1.15.jar; MeterSphereSignatureTest pause
java代码 #后续整理成python代码
import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.UUID; public class MeterSphereSignatureTest { private static final String accessKey = "Jzx7UCA0q7pr1u5l"; private static final String secretKey = "W6bkS4DbFqqa8P48"; private static String aesEncrypt(String src, String secretKey, String iv) throws Exception { byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv1 = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv1); byte[] encrypted = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8)); return Base64.encodeBase64String(encrypted); } public static void main(String[] args) { try { String signature = aesEncrypt(accessKey + "|" + UUID.randomUUID().toString() + "|" + System.currentTimeMillis(), secretKey, accessKey); System.out.println(signature); } catch (Exception e) { e.printStackTrace(); } } }

浙公网安备 33010602011771号