我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用urllib.pathname2url()。
def setUp(self): # the URLs for now which will have the WSDL files and the XSD file #urlparse.urljoin('file:', urllib.pathname2url(os.path.abspath("service.xml"))) import urllib import os from urllib.parse import urlparse from urllib.request import pathname2url query_services_url = urllib.parse.urljoin('file:', pathname2url(os.path.abspath('../wsdl_files/vipuserservices-query-1.7.wsdl'))) userservices_url = urllib.parse.urljoin('file:', pathname2url(os.path.abspath('../wsdl_files/vipuserservices-auth-1.7.wsdl'))) # initializing the Suds clients for each url, with the client certificate youll have in the same dir as this file query_services_client = Client(query_services_url, transport=HTTPSClientCertTransport('vip_certificate.crt', 'vip_certificate.crt')) user_services_client = Client(userservices_url, transport=HTTPSClientCertTransport('vip_certificate.crt', 'vip_certificate.crt')) self.test_user_services_object = SymantecUserServices(user_services_client)
def contents_url_from_path(path): """ Get github API url for contents of file from full path :param path: Path to file (<owner>/<repo>/<dir>/.../<filename>) :returns: URL suitable for a content call with github API """ owner, repo, file_path = split_full_file_path(path) # Cannot pass unicode data to pathname2url or it can raise KeyError. Must # only pass URL-safe bytes. So, something like u'\u2026' will raise a # KeyError but if we encode it to bytes, '%E2%80%A6', things work # correctly. # http://stackoverflow.com/questions/15115588/urllib-quote-throws-keyerror owner = owner.encode('utf-8') repo = repo.encode('utf-8') file_path = file_path.encode('utf-8') return urllib.pathname2url('repos/%s/%s/contents/%s' % (owner, repo, file_path))
def test_trivial(self): # A couple trivial tests self.assertRaises(ValueError, urllib2.urlopen, 'bogus url') # XXX Name hacking to get this to work on Windows. fname = os.path.abspath(urllib2.__file__).replace(os.sep, '/') # And more hacking to get it to work on MacOS. This assumes # urllib.pathname2url works, unfortunately... if os.name == 'riscos': import string fname = os.expand(fname) fname = fname.translate(string.maketrans("/.", "./")) if os.name == 'nt': file_url = "file:///%s" % fname else: file_url = "file://%s" % fname f = urllib2.urlopen(file_url) buf = f.read() f.close()
def normalize_cdmi_url(self, path): """Normalize URL path relative to current path and return. :arg path: path relative to current path :returns: absolute CDMI URL """ # Turn URL path into OS path for manipulation mypath = url2pathname(path) if not os.path.isabs(mypath): mypath = os.path.join(url2pathname(self.pwd()), mypath) # normalize path mypath = os.path.normpath(mypath) if path.endswith(os.path.sep) and not mypath.endswith(os.path.sep): mypath += os.path.sep # if isinstance(mypath, str): # mypath = mypath.encode('utf8') url = self.cdmi_url + pathname2url(mypath) return url
def test_open_url(self): if sys.version_info[0] < 3: p2url = urllib.pathname2url else: p2url = urllib2.pathname2url fpath = os.path.join(os.path.dirname(__file__), "resources") fpath = os.path.abspath(fpath) tfile = os.path.join(fpath, "rwopstest.txt") urlpath = "file:%s" % p2url(tfile) resfile = resources.open_url(urlpath) self.assertIsNotNone(resfile) tfile = os.path.join(fpath, "invalid") urlpath = "file:%s" % p2url(tfile) self.assertRaises(urllib2.URLError, resources.open_url, urlpath)
def test_trivial(self): # A couple trivial tests self.assertRaises(ValueError, urllib2.urlopen, 'bogus url') # XXX Name hacking to get this to work on Windows. fname = os.path.abspath(urllib2.__file__).replace('\\', '/') # And more hacking to get it to work on MacOS. This assumes # urllib.pathname2url works, unfortunately... if os.name == 'riscos': import string fname = os.expand(fname) fname = fname.translate(string.maketrans("/.", "./")) if os.name == 'nt': file_url = "file:///%s" % fname else: file_url = "file://%s" % fname f = urllib2.urlopen(file_url) buf = f.read() f.close()
def build_resources(self): resources = [] if not self.root_dir: return resources for root, dirs, files in os.walk(self.root_dir, followlinks=True): for file_name in files: path = os.path.join(root, file_name) if os.path.getsize(path) > MAX_FILESIZE_BYTES: continue with open(path, 'rb') as f: content = f.read() path_for_url = pathname2url(path.replace(self.root_dir, '', 1)) if self.base_url[-1] == '/' and path_for_url[0] == '/': path_for_url = path_for_url.replace('/', '' , 1) resource_url = "{0}{1}".format(self.base_url, path_for_url) resource = percy.Resource( resource_url=resource_url, sha=utils.sha256hash(content), local_path=os.path.abspath(path), ) resources.append(resource) return resources
def get_chart(symbol, normalized = "false", number_of_days = 365, data_period = 'Day', type = 'price', data_interval = 1): data = {} data['Normalized'] = normalized data['NumberOfDays'] = number_of_days data['DataPeriod'] = data_period # data['DataInterval'] = data_interval to_graph = {} to_graph['Symbol'] = symbol to_graph['Type'] = "price" to_graph['Params'] = ['c'] data['Elements'] = [to_graph] json_data = json.dumps(data) print json_data url = 'http://dev.markitondemand.com/Api/v2/InteractiveChart/json?parameters=' + urllib.pathname2url(json_data) print url response = get_json_response(url) return response
def orbit_number(self, orbit1, orbit2=None): """" Filter by the orbit number the granule was acquired during. Either a single orbit can be targeted or a range of orbits. :param orbit1: orbit to target (lower limit of range when orbit2 is provided) :param orbit2: upper limit of range :returns: Query instance """ if orbit2: self.params['orbit_number'] = quote('{},{}'.format(str(orbit1), str(orbit2))) else: self.params['orbit_number'] = orbit1 return self
def normalize_to_url(option, opt_str, value, parser): if value: if '://' not in value: # It doesn't smell like a URL. value = 'file://%s' % ( urllib.pathname2url( os.path.abspath(os.path.expanduser(value))),) if opt_str == '--download-base' and not value.endswith('/'): # Download base needs a trailing slash to make the world happy. value += '/' else: value = None name = opt_str[2:].replace('-', '_') setattr(parser.values, name, value)
def mastQuery(request): """Perform a MAST query. Parameters ---------- request (dictionary): The Mashup request json object Returns head,content where head is the response HTTP headers, and content is the returned data. """ server = 'mast.stsci.edu' # Grab Python Version version = ".".join(map(str, sys.version_info[:3])) # Create Http Header Variables headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "User-agent": "python-requests/" + version} # Encoding the request as a json string requestString = json.dumps(request) requestString = urlencode(requestString) # opening the https connection conn = httplib.HTTPSConnection(server) # Making the query conn.request("POST", "/api/v0/invoke", "request=" + requestString, headers) # Getting the response resp = conn.getresponse() head = resp.getheaders() content = resp.read().decode('utf-8') # Close the https connection conn.close() return head, content
def path2url(path): return urljoin('file:', pathname2url(path))
def path2url(path): return urljoin('file:', urllib.pathname2url(path))
def path2url(path): return urlparse.urljoin( 'file:', pathname2url(path))
def pathname2fileurl(pathname): """Returns a file:// URL for pathname. Handles OS-specific conversions.""" return urljoin('file:', pathname2url(pathname))
def generateSignature(self, data): return 'ig_sig_key_version=' + self.SIG_KEY_VERSION + '&signed_body=' + hmac.new(self.IG_SIG_KEY.encode('utf-8'), data.encode('utf-8'), hashlib.sha256).hexdigest() + '.' + urllib.pathname2url(data)
def send_msg_v2(module, token, room, msg_from, msg, msg_format='text', color='yellow', notify=False, api=NOTIFY_URI_V2): '''sending message to hipchat v2 server''' headers = {'Authorization': 'Bearer %s' % token, 'Content-Type': 'application/json'} body = dict() body['message'] = msg body['color'] = color body['message_format'] = msg_format body['notify'] = notify POST_URL = api + NOTIFY_URI_V2 url = POST_URL.replace('{id_or_name}', urllib.pathname2url(room)) data = json.dumps(body) if module.check_mode: # In check mode, exit before actually sending the message module.exit_json(changed=False) response, info = fetch_url(module, url, data=data, headers=headers, method='POST') # https://www.hipchat.com/docs/apiv2/method/send_room_notification shows # 204 to be the expected result code. if info['status'] in [200, 204]: return response.read() else: module.fail_json(msg="failed to send message, return status=%s" % str(info['status'])) # =========================================== # Module execution. #
def select(self): search = self.search sess = self.login() print('Loading search page...') for c in range(10): sess.visit(self.SEARCH_URI.format(keywords=quote(search))) sleep(20 + c) sess.interact self.client = sess if 'results-list' in sess.body(): break soup = BeautifulSoup(sess.body(), 'lxml') ul = soup.find('ul', {'class', 'results-list'}) lis = ul.findAll('li') links = dict() loop = 5 if len(lis) > 5 else len(lis) for idx in range(loop): aux = lis[idx].find('div', {'class':'search-result__info'}) link = aux.find('a', {'class':'search-result__result-link'}) link = link.get('href') if link else '??' name = aux.find('h3', {'class':'search-result__title'}) name = name.text if name else '??' desc = aux.find('p', {'class':'subline-level-1'}) desc = desc.text if desc else '??' if str(idx) not in links.keys(): links[str(idx)] = dict() links[str(idx)].update(dict(link=link, name=name, desc=desc)) choice = self.printlinks(links) return choice
def set_location(_option, _opt_str, value, parser): """Sets the location variable in the parser to the filename in question""" if not os.path.exists(os.path.abspath(value)): debug.error("The requested file doesn't exist") if parser.values.location == None: slashes = "//" # Windows pathname2url decides to convert C:\blah to ///C:/blah # So to keep the URLs correct, we only add file: rather than file:// if sys.platform.startswith('win'): slashes = "" parser.values.location = "file:" + slashes + urllib.pathname2url(os.path.abspath(value))
def constructLocalFileUrl(self, filePath): return "file://%s" % urllib.pathname2url(os.path.abspath(filePath))
def test_basic(self): # Make sure simple tests pass expected_path = os.path.join("parts", "of", "a", "path") expected_url = "parts/of/a/path" result = urllib.pathname2url(expected_path) self.assertEqual(expected_url, result, "pathname2url() failed; %s != %s" % (result, expected_url)) result = urllib.url2pathname(expected_url) self.assertEqual(expected_path, result, "url2pathame() failed; %s != %s" % (result, expected_path))
def test_quoting(self): # Test automatic quoting and unquoting works for pathnam2url() and # url2pathname() respectively given = os.path.join("needs", "quot=ing", "here") expect = "needs/%s/here" % urllib.quote("quot=ing") result = urllib.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) expect = given result = urllib.url2pathname(result) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result)) given = os.path.join("make sure", "using_quote") expect = "%s/using_quote" % urllib.quote("make sure") result = urllib.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) given = "make+sure/using_unquote" expect = os.path.join("make+sure", "using_unquote") result = urllib.url2pathname(given) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result))
def sanepathname2url(path): import urllib urlpath = urllib.pathname2url(path) if os.name == "nt" and urlpath.startswith("///"): urlpath = urlpath[2:] # XXX don't ask me about the mac... return urlpath
def _browser(file_path): import webbrowser try: from urllib import pathname2url except: from urllib.request import pathname2url webbrowser.open("file://" + pathname2url(os.path.abspath(file_path))) ## -- cookiecutter -- ##
def sanepathname2url(path): urlpath = urllib.pathname2url(path) if os.name == "nt" and urlpath.startswith("///"): urlpath = urlpath[2:] # XXX don't ask me about the mac... return urlpath
def show(self, wait=1.2, scale=10, module_color=(0, 0, 0, 255), background=(255, 255, 255, 255), quiet_zone=4): """Displays this QR code. This method is mainly intended for debugging purposes. This method saves the output of the :py:meth:`png` method (with a default scaling factor of 10) to a temporary file and opens it with the standard PNG viewer application or within the standard webbrowser. The temporary file is deleted afterwards. If this method does not show any result, try to increase the `wait` parameter. This parameter specifies the time in seconds to wait till the temporary file is deleted. Note, that this method does not return until the provided amount of seconds (default: 1.2) has passed. The other parameters are simply passed on to the `png` method. """ import os import time import tempfile import webbrowser try: # Python 2 from urlparse import urljoin from urllib import pathname2url except ImportError: # Python 3 from urllib.parse import urljoin from urllib.request import pathname2url f = tempfile.NamedTemporaryFile('wb', suffix='.png', delete=False) self.png(f, scale=scale, module_color=module_color, background=background, quiet_zone=quiet_zone) f.close() webbrowser.open_new_tab(urljoin('file:', pathname2url(f.name))) time.sleep(wait) os.unlink(f.name)
def make_file_tree(dir_path=ROOT): file_dict = {} def recur(path, list): for l in os.listdir(path): f = os.path.join(path, l) if l[0] == '.': continue elif os.path.isdir(f): list[l] = {} recur(f, list[l]) elif l.split('.')[-1] in ['py', 'txt', 'pyui', 'json']: list[l] = urllib.pathname2url(f[len(dir_path)+1:]) recur(dir_path.rstrip('/'), file_dict) return file_dict
def search_zillow_by_city_state(city, state): city_state = pathname2url('%s %s' % (city, state)) request_url = '%s/%s' % (build_url(URL, SEARCH_FOR_SALE_PATH), city_state) raw_result = search_zillow(request_url, SEARCH_XPATH_FOR_ZPID) return [x.replace('zpid_', '') for x in raw_result]