issues/488

This commit is contained in:
HIllya51 2024-01-11 11:45:13 +08:00
parent d5b3f73cef
commit 734e6782d7
2 changed files with 13 additions and 8 deletions

View File

@ -190,9 +190,11 @@ class Sessionbase:
def request(self,
method, url, params=None, data=None, headers=None,proxies=None, json=None,cookies=None, files=None,
auth=None, timeout=None, allow_redirects=True, hooks=None, stream=None, verify=False, cert=None, ):
_h=self.headers.copy()
if headers:
_h.update(headers)
headers=_h
headers=CaseInsensitiveDict(headers if headers else {})
headers.update(self.headers)
if auth and isinstance(auth,tuple) and len(auth)==2:
headers['Authorization']="Basic " + ( base64.b64encode(b":".join((auth[0].encode("latin1"), auth[1].encode("latin1")))).strip() ).decode()
@ -204,9 +206,12 @@ class Sessionbase:
_= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify)
return _
get=partialmethod(request,"GET")
post=partialmethod(request,"POST")
options=partialmethod(request,"OPTIONS")
def get(self, url, **kwargs):
return self.request("GET", url, **kwargs)
def post(self, url, **kwargs):
return self.request("POST", url, **kwargs)
def options(self, url, **kwargs):
return self.request("OPTIONS", url, **kwargs)
Sessionimpl=[Sessionbase]
def request(method, url, **kwargs):
with Sessionimpl[0]() as session:

View File

@ -13,21 +13,21 @@ class TS(basetrans):
self.checkempty(['SECRET_KEY'])
access_key_id = self.multiapikeycurrent['Access_Key']
access_key_secret = self.multiapikeycurrent['SECRET_KEY']
url = 'http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce'
url = 'http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/general'
req_body = {
"Action": "Translate",
"FormatType": "text",
"SourceLanguage": self.srclang,
"TargetLanguage": self.tgtlang,
"SourceText": query,
"Scene": "title"
"Scene": "general"
}
req_body = json.dumps(req_body)
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
nonce = str(uuid.uuid4())
md5 = hashlib.md5(req_body.encode('utf-8'))
content_md5 = base64.b64encode(md5.digest()).decode('utf-8')
stringToSign = "{method}\n{accept}\n{md5}\n{content_type}\n{date}\nx-acs-signature-method:HMAC-SHA1\nx-acs-signature-nonce:{nonce}\nx-acs-version:{version}\n{path}".format(method='POST', accept='application/json', md5=content_md5, content_type='application/json; charset=utf-8', date=date, nonce=nonce, version='2019-01-02', path='/api/translate/web/ecommerce')
stringToSign = "{method}\n{accept}\n{md5}\n{content_type}\n{date}\nx-acs-signature-method:HMAC-SHA1\nx-acs-signature-nonce:{nonce}\nx-acs-version:{version}\n{path}".format(method='POST', accept='application/json', md5=content_md5, content_type='application/json; charset=utf-8', date=date, nonce=nonce, version='2019-01-02', path='/api/translate/web/general')
key = access_key_secret.encode('utf-8')
message = stringToSign.encode('utf-8')
signed = hmac.new(key, message, digestmod=hashlib.sha1).digest()