我正在尝试通过 Python 和 v2 API 发布到 hipchat 房间。
我可以在 shell 脚本中通过 curl 毫无问题地发布:
ROOM_ID=123
AUTH_TOKEN=123456789
MESSAGE="Hello World"
curl -H "Content-Type: application/json" \
-X POST \
-d "{ \"from\": \"GTM\",
\"notify\": \"true\",
\"color\": \"red\",
\"message_format\": \"text\",
\"message\": \"$MESSAGE\"
}" \
https://hipchat.server.domain.com/v2/room/$ROOM_ID/notification?auth_token=$AUTH_TOKEN
但是,通过 Python 发送具有相同有效负载的消息失败。我使用了现成的客户端以及通过各种 http 模块的简单请求,以及这样的示例: https ://gist.github.com/bdclark/4bc8ed06643e077fa620 (当然我也搜索了 SO 本身并测试了这样的示例) .
作为一个基本示例,我尝试了例如:
host = 'hipchat.host.domain.com'
room = '123'
message = "Hello World"
headers = {'Content-type: application/json'}
color = "yellow"
format = "text"
notify=False
AUTH_TOKEN="123456789"
url = "https://{0}/v2/room/{1}/notification?auth_token={2}".format(host, room, AUTH_TOKEN)
h = httplib2.Http()
payload = {
'from':'FROM',
'message': message,
'notify': notify,
'message_format': format,
'color': color
}
resp, content = h.request(url,"POST",urllib.urlencode(payload),headers=headers)
httplib2(和基于它的客户端)返回responseNotReady
错误。请求(以及基于它的客户端)返回Connection reset by peer
错误。
由于 Curl 发送没有问题,因此 Hipchat 本身可能不是问题。我假设我的 Python 安装可能存在问题(这是 MacOs Sierra 上的默认 2.7)。所以问题是,我如何找到错误的根本原因。
非常感谢任何帮助。