Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让Python 3.x Slack(slackclient)使用公司代理_Python_Python 3.x_Proxy_Slack_Slack Api - Fatal编程技术网

让Python 3.x Slack(slackclient)使用公司代理

让Python 3.x Slack(slackclient)使用公司代理,python,python-3.x,proxy,slack,slack-api,Python,Python 3.x,Proxy,Slack,Slack Api,我有一些Python 3代码,可以使用模块slackclient发布到通道,没有问题。然而,如果我从我们公司的服务器上运行这段代码,所有流量都需要通过代理,那么它就会失败。我知道代理服务器和端口,必须使用它们从我们的服务器上运行pip,如下所示: pip install --proxy proxy.evilcorp.com:8080 slackclient sc.chat_postMessage(channel=thechannel, text=thetext, username=theuse

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

pip install --proxy proxy.evilcorp.com:8080 slackclient
sc.chat_postMessage(channel=thechannel, text=thetext, username=theusername, unfurl_links="true")
这很有效。如果我不代理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")

我只是想不出应该把代理设置放在哪里,我肯定错过了一些简单的东西。我愿意接受其他开箱即用的想法,以使这一切都能正常工作,但我无法在服务器上安装任何东西,使所有流量都通过代理或更改代理设置。

解决了这个问题。我会把这个留在这里,以防其他人也有同样的问题。看起来它是内置的,只需传入一个代理命令

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://proxies(对您来说不安全!)。然后传入一个
str
,而不是
dict
,所以只需一个代理

换个说法:

proxy = "proxy.evilcorp.com:8080"
sc = slack.WebClient(token, timeout=60, proxy=proxy)
您会注意到,实际上对api的调用也发生了变化,如下所示:

pip install --proxy proxy.evilcorp.com:8080 slackclient
sc.chat_postMessage(channel=thechannel, text=thetext, username=theusername, unfurl_links="true")