2017-08-14 119 views
0

我有一個燒瓶webapp上運行azure,我開始編碼的應用程序,一切工作完美,直到我決定提出一個請求,從YouTube的API獲取一些信息。蟒蛇燒瓶請求最終在500服務器上的錯誤蔚藍

下面的代碼:

import requests 
from flask_restful import Resource 

class GetChannelList(Resource): 
    def get(self): 
     url = "https://content.googleapis.com/youtube/v3/search?key=AIzaSyDHYcHfhnhIiuQUejcfkeorMHNQDL4Htvc&channelId=UCvS6-K6Ydmb4gH-kim3AmjA&part=snippet,id&order=date&maxResults=20" 
     return requests.get(url).content.json() 

然後在另一個Python文件,我有:

from flask import Flask 
from flask_restful import Api 
from api.get_channel_list import GetChannelList 
app = Flask(__name__) 

api = Api(app) 

api.add_resource(GetChannelList, "/api/get_channel_list") 

import FlaskWebProject1.views 

最後,我的角度控制器上我有這樣的:

$scope.GetChannelList = function(){ 
      $http.get('api/get_channel_list').then(function(data){ 
       $scope.data = data; 
      }); 
      return $scope.data; 
     } 

一切都工作正常,直到應用程序向YouTube提出請求,然後出現以下消息:

azure error

所以我決定檢查日誌,這是對他們有什麼出現:

azure error 2 azure error 3

我剛開始這個週五瓶和蔚藍的工作,我不知道接下來要做什麼來解決這個問題。任何幫助或指示什麼可以解決這個問題將不勝感激。

這是我的web.config文件:

<?xml version="1.0"?> 
<!-- Generated web.config for Microsoft Azure. Remove this comment to prevent 
    modifications being overwritten when publishing the project. 
--> 
<configuration> 
    <system.diagnostics> 
    <trace> 
     <listeners> 
     <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"> 
      <filter type="" /> 
     </add> 
     </listeners> 
    </trace> 
    </system.diagnostics> 
    <appSettings> 
    <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="FlaskWebProject1.app" /> 
    <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" value="D:\home\site\wwwroot\env\Scripts\activate_this.py" /> 
    <add key="WSGI_HANDLER" value="ptvs_virtualenv_proxy.get_virtualenv_handler()" /> 
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <handlers> 
     <add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python27\python.exe|D:\Python27\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" /> 
    </handlers> 
    <rewrite> 
     <rules> 
     <rule name="Static Files" stopProcessing="true"> 
      <match url="^/static/.*" ignoreCase="true" /> 
      <action type="Rewrite" url="^/FlaskWebProject1/static/.*" appendQueryString="true" /> 
     </rule> 
     <rule name="Configure Python" stopProcessing="true"> 
      <match url="(.*)" ignoreCase="false" /> 
      <conditions> 
      </conditions> 
      <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" /> 
     </rule> 
     </rules> 
    </rewrite> 
    </system.webServer> 
</configuration> 

編輯

以下錯誤產生:

SNIMissingWarning:HTTPS請求已經做出,但SNI (除名稱指示)擴展到TLS在此 平臺上不可用。這可能會導致服務器提供不正確的TLS 證書,這可能會導致驗證失敗。你可以升級到一個 更新版本的Python來解決這個問題。欲瞭解更多信息,請參閱 https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

InsecurePlatformWarning:一個真正的SSLContext對象不可用。 這可以防止urllib3正確配置SSL,並可能導致 某些SSL連接失敗。你可以升級到更新版本的 Python來解決這個問題。欲瞭解更多信息,請參閱 https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

InsecurePlatformWarning HTTPSConnectionPool(主機= 'content.googleapis.com',端口= 443):最大 重試網址爲突破: /YouTube的/ V3 /搜索鍵= AIzaSyCxd3KGNNiZy-omyDH7U8Lr3zGQD6ZO448 &的channelID = UCvS6-K6Ydmb4gH-kim3AmjA &部分=片斷,ID &爲了=日期&的maxResults = 20 (由SSLError(SSLError(1引起的,「_ssl.c:499:錯誤:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:證書驗證失敗'),))

+0

我對Azure或Flask不太熟悉,但是這是否回答您的問題? https://stackoverflow.com/questions/38200782/azure-flask-http-error-500-0-internal-server-error –

+0

我確實看看他的問題,但它不得不做一些事實,他是使用燒瓶的會話管理器來處理認證,這不是我的情況。 – codeninja

+1

它必須是'requests.get(url).content.json()'引起異常。響應的「內容」是「字節」。如果你想獲得json格式的響應,只需使用'requests.get(url).json()'。 – stamaimer

回答

0

我不認爲它與Azure有關。

我認爲問題在於您對YouTube的請求引發異常。

Try/Catch阻斷,並找出異常是什麼:

class GetChannelList(Resource): 
def get(self): 
    try: 
     url = "https://content.googleapis.com/youtube/v3/search?key=AIzaSyDHYcHfhnhIiuQUejcfkeorMHNQDL4Htvc&channelId=UCvS6-K6Ydmb4gH-kim3AmjA&part=snippet,id&order=date&maxResults=20" 
     return requests.get(url).content.json() 
    except Exception as e: 
     print(str(e)) 
     return {'message': 'Something went wrong'} 

知道到底出了什麼問題,請閱讀youtube data api error documentation,並抓住他們,只要你喜歡。

UPDATE

看起來像你使用Python 2,它在默認情況下自帶ssl模塊不支持SNI。你有兩個選擇。首先,Python升級到Python 3或安裝要求的安全版本:

pip install 'requests[security]' 

希望它能幫助!

+0

以json的形式返回異常並不能告訴我究竟會出現什麼問題呢? – codeninja

+0

我已經更新了我的答案,請檢查打印的內容 – codeninja

+0

@codeninja我沒有看到你的變化 –

1

當你的鏈接,SSL Warning如下表示,

SNIMissingWarning

This happens on Python 2 versions older than 2.7.9. These older versions lack SNI support. This can cause servers to present a certificate that the client thinks is invalid. Follow the pyOpenSSL guide to resolve this warning.

我通過命令python -V捻控制檯,它是Python 2.7.8上選中了Azure中的WebApp的Python2版本。因此,您可以嘗試通過Kudu站點擴展(https://<your-webapp-name>.scm.azurewebsites.net/SiteExtensions/#gallery)安裝更高版本的Python2,如下圖所示,例如Python 2.7.12 x86

enter image description here

然後,它會在路徑D:\home\Python27\安裝,你需要以更新web.config文件Python的相關配置再次嘗試燒瓶應用。

希望它有幫助。

+0

另一個問題,x86是默認的架構安裝吧? – codeninja

+0

@codeninja當然,它是默認的,除非您在應用程序設置選項卡中啓用了「64位」平臺選項。 –