From ddfdffde5f9d3b65d050f3d7b185b88a77a11605 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Thu, 15 Sep 2016 18:10:01 -0700 Subject: [PATCH 1/5] replaced pycurl with requests for webapp log --- .../azure/cli/command_modules/webapp/custom.py | 12 +++++------- src/command_modules/azure-cli-webapp/setup.py | 3 +-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py index 86c7364aeb1..e1956ad091f 100644 --- a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py +++ b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py @@ -334,10 +334,8 @@ def _get_site_credential(client, resource_group, name): return (creds.publishing_user_name, creds.publishing_password) def _stream_trace(streaming_url, user_name, password): - import pycurl - import certifi - c = pycurl.Curl() - c.setopt(c.URL, streaming_url) - c.setopt(pycurl.CAINFO, certifi.where()) - c.setopt(c.USERPWD, '{}:{}'.format(user_name, password)) - c.perform() + import requests + r = requests.get(streaming_url, auth=(user_name, password), stream=True) + for line in r.iter_lines(decode_unicode=True): + if line: + print(line) diff --git a/src/command_modules/azure-cli-webapp/setup.py b/src/command_modules/azure-cli-webapp/setup.py index 46b1ec48606..c36c646efb7 100644 --- a/src/command_modules/azure-cli-webapp/setup.py +++ b/src/command_modules/azure-cli-webapp/setup.py @@ -26,8 +26,7 @@ DEPENDENCIES = [ 'azure==2.0.0rc6', 'azure-cli-core', - 'azure-mgmt-web==0.30.0rc6', - 'pycurl' + 'azure-mgmt-web==0.30.0rc6' ] with open('README.rst', 'r', encoding='utf-8') as f: From 61bf77dd10418c7fad06c1e32ae17a0266c8dd2b Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 21 Sep 2016 15:35:05 -0700 Subject: [PATCH 2/5] Using urllib3 to implement trace_stream --- .../cli/command_modules/webapp/custom.py | 19 ++++++++++++++----- src/command_modules/azure-cli-webapp/setup.py | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py index e1956ad091f..fe193af650e 100644 --- a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py +++ b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py @@ -4,6 +4,7 @@ #--------------------------------------------------------------------------------------------- # pylint: disable=no-self-use,too-many-arguments,too-many-lines +from __future__ import print_function import threading try: from urllib.parse import urlparse @@ -334,8 +335,16 @@ def _get_site_credential(client, resource_group, name): return (creds.publishing_user_name, creds.publishing_password) def _stream_trace(streaming_url, user_name, password): - import requests - r = requests.get(streaming_url, auth=(user_name, password), stream=True) - for line in r.iter_lines(decode_unicode=True): - if line: - print(line) + import urllib3 + + urllib3.disable_warnings() + http = urllib3.PoolManager() + headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(user_name, password)) + r = http.request( + 'GET', + streaming_url, + headers=headers, + preload_content=False + ) + for chunk in r.stream(decode_content=True): + print(chunk.decode(), end='') \ No newline at end of file diff --git a/src/command_modules/azure-cli-webapp/setup.py b/src/command_modules/azure-cli-webapp/setup.py index c36c646efb7..2398dfcd7af 100644 --- a/src/command_modules/azure-cli-webapp/setup.py +++ b/src/command_modules/azure-cli-webapp/setup.py @@ -26,7 +26,8 @@ DEPENDENCIES = [ 'azure==2.0.0rc6', 'azure-cli-core', - 'azure-mgmt-web==0.30.0rc6' + 'azure-mgmt-web==0.30.0rc6', + 'urllib3==1.17' ] with open('README.rst', 'r', encoding='utf-8') as f: From a36c03bef7d7a3ef9a5f7aff618d9369ee370321 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 21 Sep 2016 16:31:53 -0700 Subject: [PATCH 3/5] minor changes --- .../azure/cli/command_modules/webapp/custom.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py index fe193af650e..c8f30c3d519 100644 --- a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py +++ b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py @@ -347,4 +347,5 @@ def _stream_trace(streaming_url, user_name, password): preload_content=False ) for chunk in r.stream(decode_content=True): - print(chunk.decode(), end='') \ No newline at end of file + if chunk: + print(chunk.decode(), end='') # each line of log has CRLF. From b04251faaf328c5bac986084ce9fbe5d5d16bcf6 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Thu, 22 Sep 2016 12:24:36 -0700 Subject: [PATCH 4/5] Fixed SSL warnings in urllib3 requests --- .../azure/cli/command_modules/webapp/custom.py | 9 +++++++-- src/command_modules/azure-cli-webapp/setup.py | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py index c8f30c3d519..1a4aaad39d8 100644 --- a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py +++ b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py @@ -335,10 +335,15 @@ def _get_site_credential(client, resource_group, name): return (creds.publishing_user_name, creds.publishing_password) def _stream_trace(streaming_url, user_name, password): + import certifi import urllib3 + try: + import urllib3.contrib.pyopenssl + urllib3.contrib.pyopenssl.inject_into_urllib3() + except ImportError: + pass - urllib3.disable_warnings() - http = urllib3.PoolManager() + http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(user_name, password)) r = http.request( 'GET', diff --git a/src/command_modules/azure-cli-webapp/setup.py b/src/command_modules/azure-cli-webapp/setup.py index 2398dfcd7af..72cea48019a 100644 --- a/src/command_modules/azure-cli-webapp/setup.py +++ b/src/command_modules/azure-cli-webapp/setup.py @@ -27,7 +27,8 @@ 'azure==2.0.0rc6', 'azure-cli-core', 'azure-mgmt-web==0.30.0rc6', - 'urllib3==1.17' + # v1.17 breaks on wildcard cert https://github.com/shazow/urllib3/issues/981 + 'urllib3[secure]==1.16' ] with open('README.rst', 'r', encoding='utf-8') as f: From 7924198b95a13bdcc72f408ae7ab7c5555064d6b Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Thu, 22 Sep 2016 18:15:49 -0700 Subject: [PATCH 5/5] handle encoding error on cmd prompt --- .../azure/cli/command_modules/webapp/custom.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py index 1a4aaad39d8..0daf90c6ebf 100644 --- a/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py +++ b/src/command_modules/azure-cli-webapp/azure/cli/command_modules/webapp/custom.py @@ -335,6 +335,7 @@ def _get_site_credential(client, resource_group, name): return (creds.publishing_user_name, creds.publishing_password) def _stream_trace(streaming_url, user_name, password): + import sys import certifi import urllib3 try: @@ -343,6 +344,7 @@ def _stream_trace(streaming_url, user_name, password): except ImportError: pass + std_encoding = sys.stdout.encoding http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(user_name, password)) r = http.request( @@ -351,6 +353,10 @@ def _stream_trace(streaming_url, user_name, password): headers=headers, preload_content=False ) - for chunk in r.stream(decode_content=True): + for chunk in r.stream(): if chunk: - print(chunk.decode(), end='') # each line of log has CRLF. + # Extra encode() and decode for stdout which does not surpport 'utf-8' + print(chunk.decode(encoding='utf-8', errors='replace') + .encode(std_encoding, errors='replace') + .decode(std_encoding, errors='replace'), end='') # each line of log has CRLF. + r.release_conn()