通过python发布有问题吗

2024-09-30 16:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点麻烦。我试着发一个帖子,试着按照文档进行操作,但我似乎做不到。在

在github上:https://github.com/trtmn/Python

欢迎拉取请求!

# Getting documentation from :
#https://docs.python.org/2/howto/urllib2.html 
import urllib
import urllib2

url = 'https://hooks.slack.com/services/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'
values = {"username": "webhookbot", "text": "This is posted to #general and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

Tags: from文档httpsimportgithubcomurldata
3条回答

使用httplib作为POST的替代:

*导入httplib

conn = httplib.HTTPSConnection(host)
conn.request('POST',urI,request_body, headers)    
response = conn.getresponse()
resp_status=response.status
resp_reason=response.reason
resp_body=response.read()
conn.close()*

看看这是否有用。在

看起来我需要把它串成JSON(我知道,但不知道怎么做)。感谢Tim G.的帮助。在

功能代码如下:

import urllib2
import json

url = 'https://hooks.slack.com/services/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'
values = {"username": "webhookbot", "text": "This is posted to #general and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}

data = json.dumps(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

不确定这是否能解决问题,但如果我在你的url末尾加上一个斜杠,当我执行你的代码时,我确实收到了一个响应。在

url='https://hooks.slack.com/services/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/'

相关问题 更多 >