无法从python为api.ai调用设置代理设置

时间:2017-06-19 10:43:37

标签: python anaconda http-proxy dialogflow

我按照官方github网站here上的说明向我的api.ai聊天机器人提出请求。以下是我收到错误的代码,解决方案应该通过代理设置调用该函数。但是,我不知道这样做的方法。

ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)
request = ai.text_request()
request.set_proxy('proxy1.company.com:8080','http')
question = input()
request.query = question
response = request.getresponse()`

我在最后一行收到以下错误。

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

请建议我如何使用代理设置。 我在Windows上使用Anaconda来运行脚本。

2 个答案:

答案 0 :(得分:1)

我想这可能有用:

首先,您需要从success设置代理:

$http.get('/api/group/specialization/' + $state.params.valueSpecialization).then(function (success) {
    $scope.groups = success.data;
    $scope.switchGroup($scope.groups[0].id) //Invoke the method
});

然后从代码中删除<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#2196f3"></solid> <corners android:radius="12dp"></corners> </shape> 行。即,

cmd

希望有所帮助

答案 1 :(得分:1)

这是我用来获取请求的一段代码,如果它们失败,那么我在代理后面发出一个新请求。我在这里指定了一些类,但你可以删除它。

class AllNames:
""" This class stores all names of directory, sites..."""

    def __init__(self):
        self.url = "http://api.openweathermap.org/data/2.5/weather?"
        self.munich_code = "id=2867714"
        self.appid = "&appid=xxxxxx"
        self.proxies = {"http": "http://user:pw@proxy.muc:8080", "https": "http://user:pw@proxy.muc:8080"}

    def get_munich_weather_data():
    """
    This module gets JSON formatted Weather data for Munich from Open Weather Map ORG and returns a Python
    representation of the data.

    :return: Python dictionnary object with weather data for Munich.
    """
        all_names = AllNames()
        complete_url = all_names.url + all_names.munich_code + all_names.appid
        try:
            req = requests.get(complete_url)
        except ConnectionError:
            req = requests.get(complete_url, proxies=all_names.proxies)
        texts = req.text
        return [ast.literal_eval(texts)]