Update smtp.py

This commit is contained in:
Namhyeon Go 2024-03-02 17:36:30 +09:00 committed by GitHub
parent dd0913cb67
commit 3e4a15a54c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

46
smtp.py
View File

@ -18,17 +18,7 @@ import requests
from decouple import config
from requests.auth import HTTPBasicAuth
def extract_credentials(url):
pattern = re.compile(r'(?P<scheme>\w+://)?(?P<username>[^:/]+):(?P<password>[^@]+)@(?P<url>.+)')
match = pattern.match(url)
if match:
scheme = match.group('scheme') if match.group('scheme') else 'https://'
username = match.group('username')
password = match.group('password')
url = match.group('url')
return username, password, scheme + url
else:
return None, None, url
from server import extract_credentials, jsonrpc2_create_id, jsonrpc2_encode, jsonrpc2_decode
try:
smtp_host = config('SMTP_HOST', default='127.0.0.1')
@ -43,40 +33,6 @@ auth = None
if _username:
auth = HTTPBasicAuth(_username, _password)
def jsonrpc2_create_id(data):
return hashlib.sha1(json.dumps(data).encode(client_encoding)).hexdigest()
def jsonrpc2_encode(method, params = None):
data = {
"jsonrpc": "2.0",
"method": method,
"params": params
}
id = jsonrpc2_create_id(data)
data['id'] = id
return (id, json.dumps(data))
def jsonrpc2_decode(data):
type, id, method, rpcdata = (None, None, None, None)
typemap = {
"params": "call",
"error": "error",
"result": "result"
}
jsondata = json.loads(data)
if jsondata['jsonrpc'] == "2.0":
for k, v in typemap.items():
if k in jsondata:
type = v
rpcdata = jsondata[k]
id = jsondata['id']
if type == "call":
method = jsondata['method']
return type, id, method, rpcdata
class CaterpillarSMTPServer(SMTPServer):
def __init__(self, localaddr, remoteaddr):
self.__class__.smtpd_hostname = "CaterpillarSMTPServer"