Python请求模块-datetime.date不可序列化

Python请求模块-datetime.date不可序列化,python,datetime,python-requests,Python,Datetime,Python Requests,我正在尝试使用Python请求模块调用一个服务,该服务返回一个包含datetime对象的Python dict 我犯了以下错误 File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 862, in _handle return route.call(**args) File "/usr/local/lib/python2.7/dist-packages/bo

我正在尝试使用Python请求模块调用一个服务,该服务返回一个包含datetime对象的Python dict

我犯了以下错误

  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1733, in wrapper
    json_response = dumps(rv)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 286, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 226, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 296, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 202, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.date(2014, 4, 16) is not JSON serializable
呼叫stmt:

r = requests.get('http://localhost:8089/allfeeds',verify=False)
print r.status_code
print r.text

您的web应用程序是使用
瓶子
编写的吗?我怎么知道

尝试从命令行进行相同的调用,例如:

$curlhttp://localhost:8089/allfeeds

我认为,这种请求的输出将完全相同

print r.text
只是打印响应-没有中断


简而言之:
requests
调用工作得很好,但是您的web服务返回一个字符串,这看起来是个问题。但问题出在您的web应用程序中,而不是客户端。

堆栈跟踪来自服务器。仅限stdlib的代码:

from urllib2 import urlopen

r = urlopen('http://localhost:8089/allfeeds')
print r.code 
print r.read().decode(r.info().getparam('charset') or 'utf-8')
会产生同样的结果<代码>r。代码应为5xx,表示“服务器错误”


datetime.date
不可JSON序列化;您应该修改服务器以将日期转换为字符串,例如,
some_date.isoformat()
或数字(POSIX timestamp),例如,
calendar.timegm(some_date.timetuple())
假设
some_date
是UTC,请参阅。

我认为返回此响应的服务必须使用
datetime.strftime
将其转换为字符串。不确定您在接收端是否可以防止此错误。gtw,不需要使用
verify=False
,因为它只影响https(并控制是否验证所用证书的有效性)。