def make_image_object_from_url(image_url): import urllib2 import socket from parse_uri import ParseUri from PIL import Image from StringIO import StringIO from django.contrib.sites.models import Site # parse url p = ParseUri() parsed_url = p.parse(image_url) # handle absolute and relative urls, Assuming http for now. if not parsed_url.host: parsed_url = p.parse(Site.objects.get_current().domain + image_url) parsed_url.protocol = "http" parsed_url.source = parsed_url.protocol + "://" + parsed_url.source request = urllib2.Request(parsed_url.source) request.add_header('User-Agent', settings.TENDENCI_USER_AGENT) opener = urllib2.build_opener() # make image object try: socket.setdefaulttimeout(1.5) data = opener.open(request).read() # get data im = Image.open(StringIO(data)) except: im = None return im
def resolve_artifact(uri): uri_parser = ParseUri() parsed_uri = uri_parser.parse(uri) if parsed_uri.protocol == "ssh": return SSHArtifact(parsed_uri.host, parsed_uri.path) elif parsed_uri.protocol == "http": return HTTPArtifact(parsed_uri.source) elif parsed_uri.protocol == "s3": return S3Artifact(parsed_uri.host, parsed_uri.path) elif parsed_uri.protocol == "mysql": return MysqlArtifact(uri) else: return FileArtifact(uri)
def url_exists(url): import socket import httplib from parse_uri import ParseUri from django.contrib.sites.models import Site # parse url p = ParseUri() parsed_url = p.parse(url) # doesn't have a host so it's relative to the website if not parsed_url.host: parsed_url = p.parse(Site.objects.get_current().domain + url) conn = httplib.HTTPConnection(parsed_url.authority) conn.request("HEAD", parsed_url.path) try: socket.setdefaulttimeout(1.5) response = conn.getresponse() if response.status == 200: return True except: return False
def __init__(self, uri): uri_parser = ParseUri() parsed_uri = uri_parser.parse(uri) params = {} if parsed_uri.host: params['host'] = parsed_uri.host if parsed_uri.user: params['user'] = parsed_uri.user if parsed_uri.password: params['passwd'] = urllib.unquote(parsed_uri.password) if parsed_uri.port: params['port'] = parsed_uri.port database, query = parsed_uri.path.split('/')[1:] params['db'] = database self.connection_params = params self.query = query self.parsed_uri = parsed_uri