def send_mail(file_new): """ 定义发邮件 :param file_new: :return: """ f = open(file_new, 'rb') mail_body = f.read() f.close() server = config.email()[0] # print(server) sender = config.email()[1] receiver = config.email()[2] name = config.email()[3] pwd = config.email()[4] msg = MIMEMultipart() # 编写html类型的邮件正文,MIMEtext()用于定义邮件正文 # 发送正文 text = MIMEText(mail_body, 'html', 'utf-8') text['Subject'] = Header('自动化测试报告', 'utf-8') msg.attach(text) # 发送附件 # Header()用于定义邮件标题 msg['Subject'] = Header('自动化测试报告', 'utf-8') msg_file = MIMEText(mail_body, 'html', 'utf-8') msg_file['Content-Type'] = 'application/octet-stream' msg_file["Content-Disposition"] = 'attachment; filename=WbfOpenApi_TestReport.html' msg.attach(msg_file) msg['from'] = sender # 发送邮件的人 msg['to'] = receiver # smtp = smtplib.SMTP() smtp = smtplib.SMTP_SSL(server, 465) # smtp.connect(server, 587) # smtp.connect(server, 465) # smtp.ehlo() # smtp.starttls() # smtp.ehlo() smtp.login(name, pwd) smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) smtp.quit() try: print('The email has been sent successfully !') except Exception as e: wirte_log.return_log(name, pwd, e)
def lastprice(self, symbol, sex): """ 获取最新成交价 :param symbol: :return: """ url = environment(config.env_name, sex)[-2] + '/open/api/market' # print(url) params = {"symbol": symbol} # print(params) try: result = requests.get(url, params=params) if result.status_code == 200: last_price = result.json()['data'][symbol] wirte_log.return_log(params, url, last_price) print(last_price) return last_price else: wirte_log.return_log(params, url, result) except Exception as e: print("error:{}".format(e))
def market_depth(self, sym, typ, sex): """ 获取市场买卖盘 :return: """ request_path = '/open/api/market_dept' host = environment(config.env_name, sex)[-2] # print(host) url = host + request_path print(url) params = {"symbol": sym, "type": typ} print(params) try: res = requests.get(url=url, params=params) # print(res) if res.status_code == 200: r = res.json() wirte_log.return_log(params, url, r) print(r) return r else: wirte_log.return_log(params, url, res) except Exception as e: # print('error:{}'.format(e)) wirte_log.return_log(params, url, e)
def account_balance(self, data, currency): """ 查询账户资产 :return: """ api_key = environment(data)[0] secret_key = environment(data)[1] host = environment(data)[-2] # print(host) tie = int(time.time()) request_path = '/open/api/user/account' result = wbf_signature.Signature(api_key, secret_key, tie).get_sign(request_path, host) # print(result) coin = result['data']['coin_list'] # print(coin) for i in range(0, len(coin)): if coin[i]['coin'] == currency: # print(coin[i]['normal'], coin[i]['locked']) wirte_log.return_log(currency, coin[i]['normal'], coin[i]['locked']) # print(coin[i]['normal'], coin[i]['locked']) return coin[i]['normal'], coin[i]['locked']
def get(self, request_path, host, p): params = {"api_key": self.api_key, "time": self.tie} p.update(params) si = self.sign(p) url = host + request_path p['sign'] = si try: res = requests.get(url=url, params=p) if res.status_code == 200: r = res.json() wirte_log.return_log(p, url, r) return r else: wirte_log.return_log(p, url, res) except Exception as e: wirte_log.return_log(p, url, e)
def post_sign(self, p, request_path, host): url = host + request_path params = {"api_key": self.api_key, "time": self.tie} p.update(params) si = self.sign(p) p['sign'] = si # print("请求参数:{}".format(p)) try: res = requests.post(url=url, data=p, headers={'content-type': "application/x-www-form-urlencoded", 'cache-control': "no-cache"}) if res.status_code == 200: r = res.json() wirte_log.return_log(p, url, r) return r else: wirte_log.return_log(p, url, res) except Exception as e: wirte_log.return_log(p, url, e)