sni*_*erd 2 python proxy python-3.x slack-api slack
我有一些Python 3代码,可以使它使用模块slackclient发布到频道,没问题。但是,如果我从所有流量都需要通过代理的公司服务器中运行此代码,它将失败。我知道代理服务器和端口,必须使用它们从我们的服务器运行pip,如下所示:
pip install --proxy proxy.evilcorp.com:8080 slackclient
Run Code Online (Sandbox Code Playgroud)
效果很好。如果我不使用代理,则无法按预期连接。所以这告诉我,我只需要弄清楚如何获取我的slackclient代码以使用代理,但是如何呢?这是我的代码:
from slackclient import SlackClient
def get_slackclient():
token = "blah-blah-token"
sc = SlackClient(token)
return sc
def post_slackmessage(username,channel,text):
sc = get_slackclient()
try:
sc.api_call("chat.postMessage",channel=channel,text=text,username=username,unfurl_links="true")
except:
print ("failed to post messaage to slack")
post_slackmessage("test_slack", "test", "hurrah it posted")
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚放置代理设置的位置,我肯定缺少一些简单的东西。我乐于接受其他开箱即用的想法,以使所有这些工作正常进行,但是我真的不能在服务器上安装任何东西以使所有流量都通过代理或更改代理设置。
弄清楚了。如果有人遇到同样的问题,我将在此保留。看起来像是内置的,只需传入代理字典即可。
def get_slackclient():
#https://api.slack.com/custom-integrations/legacy-tokens
token = "blah-blah-blah"
proxies = dict(https="proxy.evilcorp.com:8080", http="proxy.evilcorp.com:8080")
sc = SlackClient(token, proxies=proxies)
return sc
Run Code Online (Sandbox Code Playgroud)
好吧,那很容易:)
更新
如果您恰巧要升级到最新的Slack模块,则有所不同,仅支持http://代理(对您而言不安全!)。然后您传入一个str而不是dict一个代理。
只需更改为:
proxy = "proxy.evilcorp.com:8080"
sc = slack.WebClient(token, timeout=60, proxy=proxy)
Run Code Online (Sandbox Code Playgroud)
您会注意到,实际上对api的调用也已更改,如下所示:
sc.chat_postMessage(channel=thechannel, text=thetext, username=theusername, unfurl_links="true")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
705 次 |
| 最近记录: |