PYTHON 联接钉钉,向群发送信息报错。请解

tangowp251 2018-01-23 11:50:44
代码如下:
#!/usr/bin/python
#coding=utf-8
import urllib.request
import json
import sys
import socket
import ssl

# 获取钉钉消息
def extractionMessage() :
#拼接需要发送的消息
return "##### <font color=orange> message </font>"

#发送钉钉消息
def sendDingDingMessage(url, data):
req = urllib.request.Request(url)
req.add_header("Content-Type", "application/json; charset=utf-8")
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
response = opener.open(req, json.dumps(data))
return response.read()

#主函数
def main():
posturl = "https://oapi.dingtalk.com/robot/send?access_token="
data = {"msgtype": "markdown", "markdown": {"text": extractionMessage(),"title":"Jenkins","isAtAll": "false"}}
sendDingDingMessage(posturl, data)

if __name__=="__main__":
main()

报错信息如下
File "ding.py", line 29, in <module>
File "ding.py", line 26, in main
File "ding.py", line 19, in sendDingDingMessage
File "/usr/local/lib/python3.6/urllib/request.py", line 524, in open
req = meth(req)
File "/usr/local/lib/python3.6/urllib/request.py", line 1248, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It
cannot be of type str.


python 版本是3.6.4
...全文
597 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
混沌鳄鱼 2018-01-23
  • 打赏
  • 举报
回复
response = opener.open(req, json.dumps(data)) 改成 response = opener.open(req, json.dumps(data).encode('utf-8'))

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧