【问题标题】:make Python 3.x Slack (slackclient) use a corporate proxy使 Python 3.x Slack (slackclient) 使用公司代理
【发布时间】:2017-09-22 13:03:56
【问题描述】:

我有一些 Python 3 代码,可以让它使用模块 slackclient 发布到频道,没问题。但是,如果我从所有流量都需要通过代理的公司服务器运行此代码,则会失败。我知道代理服务器和端口,必须使用它们从我们的服务器运行 pip,如下所示:

pip install --proxy proxy.evilcorp.com:8080 slackclient

效果很好。如果我不代理 pip,它将无法按预期连接。所以这告诉我我只需要弄清楚如何让我的 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")

我似乎不知道在哪里放置代理设置,我一定是遗漏了一些简单的东西。我对其他开箱即用的想法持开放态度,以使这一切正常运行,但我无法在服务器上安装任何东西以使所有流量都通过代理或更改代理设置。

【问题讨论】:

    标签: python python-3.x proxy slack slack-api


    【解决方案1】:

    想通了。我会把这个留在这里,以防其他人有同样的问题。看起来它是内置的,只需传入一个代理字典即可。

    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
    

    嗯,这很容易:)

    更新

    如果您碰巧升级到最新的 slack 模块,情况会有所不同,并且仅支持 http:// 代理(对您来说不安全!)。你传入一个str 而不是dict 所以只有一个代理。

    改成这样:

    proxy = "proxy.evilcorp.com:8080"
    sc = slack.WebClient(token, timeout=60, proxy=proxy)
    

    您会注意到,对 api 的实际调用也发生了变化,如下所示:

    sc.chat_postMessage(channel=thechannel, text=thetext, username=theusername, unfurl_links="true")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2014-10-28
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多