random1111.py 3.6 KB

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