【问题标题】:How to compare HTTP headers with time fields in Python?如何将 HTTP 标头与 Python 中的时间字段进行比较?
【发布时间】:2016-09-29 09:26:53
【问题描述】:

我正在使用 Python 构建代理服务器作为练习和 我想比较从服务器接收到的两个不同的时间字符串。 例如,

Date: Sun, 24 Nov 2013 18:34:30 GMT
Expires: Sat, 23 Nov 2013 18:34:30 GMT

如何比较到期时间是否早于当前时间?我是否必须使用datetime 模块的strptime 方法解析它,或者有更简单的方法吗?

【问题讨论】:

    标签: python datetime http-headers compare http-proxy


    【解决方案1】:

    将每个字符串转换为时间戳并进行比较,例如如下:

    from datetime import datetime    
    
    date1 = "Date: Sun, 24 Nov 2013 18:34:30 GMT"
    date2 = "Expires: Sat, 23 Nov 2013 18:34:30 GMT"
    format = "%a, %d %b %Y %H:%M:%S %Z"
    
    if datetime.strptime(date1, "Date: " + format) >= datetime.strptime(date2, "Expires: " + format):
        print "Expired"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2022-09-27
      • 1970-01-01
      • 2017-07-02
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多