|
import requests |
|
import os |
|
from .utils.AuthV3Util import addAuthParams |
|
|
|
|
|
APP_KEY = os.environ.get("APP_KEY",None) |
|
|
|
|
|
APP_SECRET = os.environ.get("APP_SECRET",None) |
|
|
|
|
|
def createRequest(msq): |
|
''' |
|
note: 将下列变量替换为需要请求的参数 |
|
''' |
|
q = msq |
|
lang_from = 'auto' |
|
lang_to = 'en' |
|
|
|
|
|
data = {'q': q, 'from': lang_from, 'to': lang_to} |
|
|
|
addAuthParams(APP_KEY, APP_SECRET, data) |
|
|
|
header = {'Content-Type': 'application/x-www-form-urlencoded'} |
|
res = doCall('https://openapi.youdao.com/api', header, data, 'post') |
|
print(str(res.content, 'utf-8')) |
|
return str(res.content, 'utf-8') |
|
|
|
|
|
def doCall(url, header, params, method): |
|
if 'get' == method: |
|
return requests.get(url, params) |
|
elif 'post' == method: |
|
return requests.post(url, params, header) |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
createRequest() |
|
|