本文整理汇总了Python中email.utils.format_datetime方法的典型用法代码示例。如果您正苦于以下问题:Python utils.format_datetime方法的具体用法?Python utils.format_datetime怎么用?Python utils.format_datetime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.utils
的用法示例。
在下文中一共展示了utils.format_datetime方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def __call__(self, request):
# We have to format the date according to RFC 2616
# https://tools.ietf.org/html/rfc2616#section-14.18
now = datetime.now(timezone.utc)
date = format_datetime(now, True)
try:
# Attempt to decode request.body (assume bytes received)
msg = '\n'.join([self.serial.lower(), date, request.body.decode('utf8')])
except AttributeError:
# Decode failed, assume request.body is already type str
msg = '\n'.join([self.serial.lower(), date, request.body])
signing = hmac.new(key=self.secret.encode('utf8'),
msg=msg.encode('utf8'),
digestmod=hashlib.sha256)
request.headers['Date'] = date
request.headers['Authorization'] = "NEATOAPP " + signing.hexdigest()
return request
示例2: render_js
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def render_js(request, template_name, cache=True, *args, **kwargs):
response = render(request, template_name, *args, **kwargs)
response["Content-Type"] = \
"application/javascript; charset=UTF-8"
if cache:
now = datetime.now(timezone.utc)
response["Last-Modified"] = format_datetime(now,
usegmt=True)
# cache in the browser for 1 month
expires = now + timedelta(days=31)
response["Expires"] = format_datetime(expires,
usegmt=True)
else:
response["Pragma"] = "No-Cache"
return response
示例3: parse
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_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'])
示例4: test_naive_datetime
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def test_naive_datetime(self):
self.assertEqual(utils.format_datetime(self.naive_dt),
self.datestring + ' -0000')
示例5: test_aware_datetime
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def test_aware_datetime(self):
self.assertEqual(utils.format_datetime(self.aware_dt),
self.datestring + self.offsetstring)
示例6: test_usegmt
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def test_usegmt(self):
utc_dt = datetime.datetime(*self.dateargs,
tzinfo=datetime.timezone.utc)
self.assertEqual(utils.format_datetime(utc_dt, usegmt=True),
self.datestring + ' GMT')
示例7: test_usegmt_with_naive_datetime_raises
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_datetime [as 别名]
def test_usegmt_with_naive_datetime_raises(self):
with self.assertRaises(ValueError):
utils.format_datetime(self.naive_dt, usegmt=True)
示例8: download_list
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import format_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()