random1111.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import base64
  2. import hashlib
  3. import os
  4. import random
  5. import time
  6. # import rsa as rsa
  7. import yaml
  8. from common.request_util import *
  9. from common.yaml_util import read_yaml
  10. class DubugTalk:
  11. # 获得随机数
  12. def get_random_number(self, min, max):
  13. return str(random.randint(int(min), int(max)))
  14. def read_yaml(self, key):
  15. return read_yaml(key)
  16. # Md5加密
  17. def md5(self, args):
  18. # 以指定的编码格式编码字符串
  19. utf8_str = str(args).encode("utf-8")
  20. # MD5加密(哈稀算法)
  21. md5_str = hashlib.md5(utf8_str).hexdigest()
  22. return md5_str.upper()
  23. # base64加密
  24. def bs64(self, args):
  25. # 以指定的编码格式编码字符串
  26. utf8_str = str(args).encode("utf-8")
  27. # base64加密
  28. ba64_str = base64.b64encode(utf8_str).decode('utf-8')
  29. return ba64_str.upper()
  30. # RSA双钥加密
  31. # # 生成公钥私钥到指定文件
  32. # def create_key(self):
  33. # # 根据密钥长度生成公钥私钥
  34. # (public_key, private_key) = rsa.newkeys(1024)
  35. # # 报存公钥
  36. # with open("public.pem", "w+") as f:
  37. # f.write(public_key.save_pkcs1().decode())
  38. # # 报存私钥
  39. # with open("private.pem", "w+") as f:
  40. # f.write(private_key.save_pkcs1().decode())
  41. # 通过公钥加密
  42. # def public_key_jiami(self, args):
  43. # # 导入密钥
  44. # with open("public.pem") as f:
  45. # pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
  46. # # 加密
  47. # byte_str = rsa.encrypt(str(args).encode("utf-8"), pubkey)
  48. # # 把二进制转化成字符串格式
  49. # miwen = base64.b64encode(byte_str).decode("utf-8")
  50. # return miwen
  51. # 通过私钥解密
  52. # def private_key_jiemi(self,args):
  53. # with open("private.pem") as f:
  54. # prikey = rsa.PrivateKey.load_pkcs1(f.read().encode())
  55. # # 把字符串转换二进制
  56. # byte_key = base64.b64decode(args)
  57. # #解密
  58. # mingwen = rsa.decrypt(byte_key, prikey).decode()
  59. # return mingwen
  60. # singe签名
  61. def singes(self, yaml_path):
  62. last_url = ""
  63. last_data = {}
  64. with open(os.getcwd() + yaml_path, encoding="utf-8") as f:
  65. yaml_value = yaml.load(f, Loader=yaml.FullLoader)
  66. for caseinfo in yaml_value:
  67. # print(caseinfo)
  68. caseinfo_keys = caseinfo.keys()
  69. # 判断一级关键字是否存在:name,request,validate
  70. if "request" in caseinfo_keys:
  71. # 判断url
  72. if "url" in caseinfo['request'].keys():
  73. last_url = caseinfo['request']['url']
  74. print(caseinfo['request']['url'])
  75. # 判断参数
  76. req = caseinfo['request']
  77. for key, value in req.items():
  78. if key in ["params", "data", "json"]:
  79. for p_key, p_value in req[key].items():
  80. last_data[p_key] = p_value
  81. last_url = last_url[last_url.index("?") + 1:len(last_url)]
  82. # 把url字典格式加入到last_data字典格式里面
  83. lis = last_url.split("&")
  84. print(lis)
  85. for a in lis:
  86. last_data[a[0:a.index('=')]] = a[a.index('=') + 1:len(a)]
  87. from common.request_util import RequestUtil
  88. # last_data = RequestUtil(self).replace_value(last_data)
  89. print(last_data)