本文整理汇总了Python中email.utils.parsedate_to_datetime方法的典型用法代码示例。如果您正苦于以下问题:Python utils.parsedate_to_datetime方法的具体用法?Python utils.parsedate_to_datetime怎么用?Python utils.parsedate_to_datetime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.utils
的用法示例。
在下文中一共展示了utils.parsedate_to_datetime方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: date
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def date(self) -> Optional[datetime]:
if "date" in self.headers:
return parsedate_to_datetime(self.headers["date"])
else:
return None
示例2: if_modified_since
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def if_modified_since(self) -> Optional[datetime]:
if "If-Modified-Since" in self.headers:
return parsedate_to_datetime(self.headers["If-Modified-Since"])
else:
return None
示例3: if_unmodified_since
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def if_unmodified_since(self) -> Optional[datetime]:
if "If-Unmodified-Since" in self.headers:
return parsedate_to_datetime(self.headers["If-Unmodified-Since"])
else:
return None
示例4: date
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def date(self) -> Optional[datetime]:
try:
return parsedate_to_datetime(self.headers.get("Date", ""))
except TypeError: # Not a date format
return None
示例5: expires
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def expires(self) -> Optional[datetime]:
try:
return parsedate_to_datetime(self.headers.get("Expires", ""))
except TypeError: # Not a date format
return None
示例6: retry_after
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def retry_after(self) -> Optional[datetime]:
value = self.headers.get("Retry-After", "")
if value.isdigit():
return datetime.utcnow() + timedelta(seconds=int(value))
else:
try:
return parsedate_to_datetime(value)
except TypeError:
return None
示例7: natural_last_modified
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def natural_last_modified(self):
last_modified = parsedate_to_datetime(self.last_modified)
now = datetime.now(timezone.utc)
tense = "from now" if last_modified > now else "ago"
return "{0} {1}".format(humanize.naturaldelta(now - last_modified), tense)
示例8: parse
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def parse(cls, value, kwds):
if not value:
kwds['defects'].append(errors.HeaderMissingRequiredValue())
kwds['datetime'] = None
kwds['decoded'] = ''
kwds['parse_tree'] = parser.TokenList()
return
if isinstance(value, str):
value = utils.parsedate_to_datetime(value)
kwds['datetime'] = value
kwds['decoded'] = utils.format_datetime(kwds['datetime'])
kwds['parse_tree'] = cls.value_parser(kwds['decoded'])
示例9: test_parsedate_to_datetime
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def test_parsedate_to_datetime(self):
self.assertEqual(
utils.parsedate_to_datetime(self.datestring + self.offsetstring),
self.aware_dt)
示例10: test_parsedate_to_datetime_naive
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def test_parsedate_to_datetime_naive(self):
self.assertEqual(
utils.parsedate_to_datetime(self.datestring + ' -0000'),
self.naive_dt)
示例11: download_list
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def download_list(url):
headers = None
cache = Path(config['cache'], hashlib.sha1(url.encode()).hexdigest())
if cache.is_file():
last_modified = datetime.utcfromtimestamp(cache.stat().st_mtime)
headers = {
'If-modified-since': eut.format_datetime(last_modified),
'User-Agent': 'Bind adblock zonfile updater v1.0 (https://github.com/Trellmor/bind-adblock)'
}
try:
r = requests.get(url, headers=headers, timeout=config['req_timeout_s'])
if r.status_code == 200:
with cache.open('w', encoding='utf8') as f:
f.write(r.text)
if 'last-modified' in r.headers:
last_modified = eut.parsedate_to_datetime(r.headers['last-modified']).timestamp()
os.utime(str(cache), times=(last_modified, last_modified))
return r.text
except requests.exceptions.RequestException as e:
print(e)
if cache.is_file():
with cache.open('r', encoding='utf8') as f:
return f.read()
示例12: processMessages
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_to_datetime [as 别名]
def processMessages(self, msgs):
log = []
for msg in msgs:
CCemail = None
to_email = None
header_data = msg["payload"]["headers"]
for data in header_data:
if "To" == data["name"]:
to_email = data["value"]
if '<' in to_email:
start = to_email.find('<')
end = to_email.find('>')
to_email = to_email[start + 1: end]
if "From" == data["name"]:
email_id = data["value"]
if '<' in email_id:
start = email_id.find('<')
end = email_id.find('>')
email_id = email_id[start + 1: end]
if "Received" == data["name"]:
timestamp = parsedate_to_datetime(data["value"].split(';', 1)[-1]).astimezone(
pytz.timezone("Asia/Calcutta"))
if "Cc" == data["name"]:
CCemail = data["value"]
if '<' in CCemail:
start = CCemail.find('<')
end = CCemail.find('>')
CCemail = CCemail[start + 1: end]
MsgB64 = ""
try:
if "parts" in msg["payload"]:
MsgB64 = msg["payload"]['parts'][0]['body']['data'].replace("-", "+").replace("_", "/")
else:
MsgB64 = msg["payload"]['body']['data'].replace("-", "+").replace("_", "/")
except Exception as e:
errorObj = Errors.objects.create(module='status', errorContent=e, timestamp=datetime.now())
errorObj.save()
Msg = base64.b64decode(bytes(MsgB64, 'UTF-8')).decode('UTF-8')
Msg = "<br />".join(Msg.split("\r\n"))
Msg = Msg.split("wrote:<br /><br />>")[0]
Msg = Msg.rsplit("On ", 1)[0]
Msg = Msg.split("-- <br />You received this message")[0]
log.append({
"CCemail": CCemail,
"to": to_email,
'email': email_id,
'date': self.date,
'timestamp': timestamp.isoformat(),
'message': Msg
})
return log