[Kimchi-devel] [PATCH v2 2/3] Fix verification of remote ISO
Royce Lv
lvroyce at linux.vnet.ibm.com
Mon Aug 11 02:34:30 UTC 2014
On 2014年08月09日 03:34, Christy Perez wrote:
> Currently the entire ISO is grabbed, which times out.
> Use httplib to connect to the server and just get
> the head of the http object.
>
> Signed-off-by: Christy Perez <christy at linux.vnet.ibm.com>
> ---
> src/kimchi/utils.py | 21 +++++++++++++++------
> 1 file changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
> index 9ed8802..4ab0435 100644
> --- a/src/kimchi/utils.py
> +++ b/src/kimchi/utils.py
> @@ -26,11 +26,11 @@
> import re
> import subprocess
> import traceback
> -import urllib2
> import xml.etree.ElementTree as ET
> +from httplib import HTTPConnection, HTTPException
> from multiprocessing import Process, Queue
> from threading import Timer
> -
> +from urlparse import urlparse
> from cherrypy.lib.reprconf import Parser
>
> from kimchi.asynctask import AsyncTask
> @@ -138,14 +138,23 @@ def import_module(module_name):
> return __import__(module_name, globals(), locals(), [''])
>
>
> +def url_to_fqdn(path):
> + parse_result = urlparse(path)
> + return parse_result.netloc
> +
> +
> def check_url_path(path):
> try:
> - code = urllib2.urlopen(path).getcode()
> - if code != 200:
> + server_name = url_to_fqdn(path)
> + conn = HTTPConnection(server_name)
> + # Don't try to get the whole file:
> + conn.request('HEAD', path)
> + response = conn.getresponse()
> + conn.close()
> + if response.status != 200:
> return False
> - except (urllib2.URLError, ValueError):
> + except (HTTPException, IOError, ValueError):
> return False
> -
> return True
>
>
Neat fix, just one question, do we need to close the 'conn' in finally
block when an HTTPException happens like below? or the HTTPException
just happens before we establish the connection so we don't need to
close it?
def check_url_path(path):
conn = None
try:
- code = urllib2.urlopen(path).getcode()
- if code != 200:
+ server_name = url_to_fqdn(path)
+ conn = HTTPConnection(server_name)
+ # Don't try to get the whole file:
+ conn.request('HEAD', path)
+ response = conn.getresponse()
+ if response.status != 200:
return False
- except (urllib2.URLError, ValueError):
+ except (HTTPException, IOError, ValueError):
return False
+ finally:
if conn != None:
conn.close()
-
return True
More information about the Kimchi-devel
mailing list