本文整理汇总了Python中urllib.splitport方法的典型用法代码示例。如果您正苦于以下问题:Python urllib.splitport方法的具体用法?Python urllib.splitport怎么用?Python urllib.splitport使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib
的用法示例。
在下文中一共展示了urllib.splitport方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def request(self, method, url, body=None, headers={}):
# Request is called before connect, so can interpret url and get
# real host/port to be used to make CONNECT request to proxy
proto, rest = urllib.splittype(url)
if proto is None:
raise ValueError, "unknown URL type: %s" % url
# Get host
host, rest = urllib.splithost(rest)
# Try to get port
host, port = urllib.splitport(host)
# If port is not defined try to get from proto
if port is None:
try:
port = self._ports[proto]
except KeyError:
raise ValueError, "unknown protocol for: %s" % url
self._real_host = host
self._real_port = int(port)
httplib.HTTPConnection.request(self, method, url, body, headers)
示例2: ftp_open
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def ftp_open(self, req):
host = req.get_host()
if not host:
raise IOError, ('ftp error', 'no host given')
host, port = splitport(host)
if port is None:
port = ftplib.FTP_PORT
else:
port = int(port)
# username/password handling
user, host = splituser(host)
if user:
user, passwd = splitpasswd(user)
else:
passwd = None
host = unquote(host)
user = unquote(user or '')
passwd = unquote(passwd or '')
try:
host = socket.gethostbyname(host)
except socket.error, msg:
raise urllib2.URLError(msg)
示例3: _get_proxy_info
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def _get_proxy_info(self, scheme, authority):
"""Return a ProxyInfo instance (or None) based on the scheme
and authority.
"""
hostname, port = urllib.splitport(authority)
proxy_info = self.proxy_info
if callable(proxy_info):
proxy_info = proxy_info(scheme)
if hasattr(proxy_info, "applies_to") and not proxy_info.applies_to(hostname):
proxy_info = None
return proxy_info
示例4: _get_proxy_info
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def _get_proxy_info(self, scheme, authority):
"""Return a ProxyInfo instance (or None) based on the scheme
and authority.
"""
hostname, port = urllib.splitport(authority)
proxy_info = self.proxy_info
if callable(proxy_info):
proxy_info = proxy_info(scheme)
if (hasattr(proxy_info, 'applies_to')
and not proxy_info.applies_to(hostname)):
proxy_info = None
return proxy_info
示例5: _spliturl
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def _spliturl(url):
scheme, opaque = urllib.splittype(url)
netloc, path = urllib.splithost(opaque)
host, port = urllib.splitport(netloc)
# Strip brackets if its an IPv6 address
if host.startswith('[') and host.endswith(']'): host = host[1:-1]
if port is None: port = DEFAULT_PORT
return scheme, host, port, path
# Given an HTTP request handler, this wrapper objects provides a related
# family of convenience methods built using that handler.
示例6: test_splitport
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def test_splitport(self):
splitport = urllib.splitport
self.assertEqual(splitport('parrot:88'), ('parrot', '88'))
self.assertEqual(splitport('parrot'), ('parrot', None))
self.assertEqual(splitport('parrot:'), ('parrot', None))
self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None))
self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None))
self.assertEqual(splitport('[::1]:88'), ('[::1]', '88'))
self.assertEqual(splitport('[::1]'), ('[::1]', None))
self.assertEqual(splitport(':88'), ('', '88'))
示例7: test_splitport
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def test_splitport(self):
splitport = urllib.splitport
self.assertEqual(splitport('parrot:88'), ('parrot', '88'))
self.assertEqual(splitport('parrot'), ('parrot', None))
self.assertEqual(splitport('parrot:'), ('parrot', None))
self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None))
self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None))
示例8: url_permutations
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def url_permutations(url):
"""Try all permutations of hostname and path which can be applied
to blacklisted URLs
"""
def url_host_permutations(host):
if re.match(r'\d+\.\d+\.\d+\.\d+', host):
yield host
return
parts = host.split('.')
l = min(len(parts), 5)
if l > 4:
yield host
for i in range(l - 1):
yield '.'.join(parts[i - l:])
def url_path_permutations(path):
yield path
query = None
if '?' in path:
path, query = path.split('?', 1)
if query is not None:
yield path
path_parts = path.split('/')[0:-1]
curr_path = ''
for i in range(min(4, len(path_parts))):
curr_path = curr_path + path_parts[i] + '/'
yield curr_path
protocol, address_str = urllib.splittype(url)
host, path = urllib.splithost(address_str)
user, host = urllib.splituser(host)
host, port = urllib.splitport(host)
host = host.strip('/')
seen_permutations = set()
for h in url_host_permutations(host):
for p in url_path_permutations(path):
u = '{}{}'.format(h, p)
if u not in seen_permutations:
yield u
seen_permutations.add(u)
示例9: SplitUrl
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def SplitUrl(url):
Url = collections.namedtuple('Url', ('method host port path'))
method, rest = urllib.splittype(url)
hostport, path = urllib.splithost(rest)
host, port = urllib.splitport(hostport)
return Url(method, host, int(port or 0), path)
示例10: _tomcatFsStartFunc
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def _tomcatFsStartFunc(self, event):
if (str(self._tomcatMainURL.getText()).strip()) != '':
import urllib
url = str(self._tomcatMainURL.getText().strip())
if url.startswith('http://') or url.startswith('https://'):
if not url.endswith('/'):
url += '/'
proto, rest = urllib.splittype(url)
host, rest = urllib.splithost(rest)
host2, port = urllib.splitport(host)
host = host.split(':')[0]
if port is None:
port = 80
usersFile = str(self._tomcatMainUname.getText())
pwdsFile = str(self._tomcatMainPwd.getText())
if usersFile != '' and pwdsFile != '':
bruter = Bruter(host, port, usersFile, pwdsFile)
print bruter.doLogin()
else:
print 'Please load dict files'
else:
print 'Check URL'
# ##########################################################
示例11: uri_encode_idna
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def uri_encode_idna(uri):
'''
Do IDNA encoding for hostnames, if possible
'''
scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
if scheme.lower() in urlparse.uses_netloc and netloc is not None:
user_password, host_port = urllib.splituser(netloc)
if host_port is not None:
host, port = urllib.splitport(host_port)
if host is not None and host[:1] + host[-1:] != '[]':
# NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
host = ''.join([ chr(ord(ch)) for ch in host ])
try:
host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
except:
pass
try:
host = urllib.quote(encode_idna(urllib.unquote(host)))
except:
pass
host_port = host + (port is not None and (':' + port) or '')
netloc = (user_password is not None and (user_password + '@') or '') + host_port
pass
uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
# NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
uri = ''.join([ chr(ord(ch)) for ch in uri ])
return uri
示例12: uri_decode_idna
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def uri_decode_idna(uri):
'''
Do IDNA decoding for hostnames, if possible
'''
scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
if scheme.lower() in urlparse.uses_netloc and netloc is not None:
user_password, host_port = urllib.splituser(netloc)
if host_port is not None:
host, port = urllib.splitport(host_port)
if host is not None and host[:1] + host[-1:] != '[]':
# NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
host = ''.join([ chr(ord(ch)) for ch in host ])
try:
host = urllib.quote(decode_idna(urllib.unquote(host)))
except:
pass
try:
host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
except:
pass
host_port = host + (port is not None and (':' + port) or '')
netloc = (user_password is not None and (user_password + '@') or '') + host_port
pass
uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
# NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
uri = ''.join([ chr(ord(ch)) for ch in uri ])
return uri
示例13: normalize_uri
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def normalize_uri(uri):
'''
normalize a URI; also converts IRIs to URIs
'''
uri = ''.join([ (ord(x) in xrange(33, 127)) and x or urllib.quote(x, safe='') for x in u''.join(uri.decode('UTF-8').split()).encode('UTF-8') ])
try:
scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
if scheme in urlparse.uses_netloc:
if netloc is not None:
user, hostport = urllib.splituser(netloc)
if hostport is not None:
host, port = urllib.splitport(hostport)
if host is not None:
if host[:1] != '[':
# hostname segments get downcased and IDNA-encoded
ohost = []
for part in host.split('.'):
part = urllib.unquote(part)
try:
part = part.decode('UTF-8').lower().encode('UTF-8')
try:
part = part.decode('UTF-8').encode('idna')
pass
except KeyboardInterrupt, k:
raise
except:
pass
pass
except KeyboardInterrupt, k:
raise
except:
示例14: open_local_file
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import splitport [as 别名]
def open_local_file(self, req):
import mimetypes
import mimetools
host = req.get_host()
file = req.get_selector()
localfile = urllib.url2pathname(file)
stats = os.stat(localfile)
size = stats[stat.ST_SIZE]
modified = rfc822.formatdate(stats[stat.ST_MTIME])
mtype = mimetypes.guess_type(file)[0]
if host:
host, port = urllib.splitport(host)
if port or socket.gethostbyname(host) not in self.get_names():
raise urllib2.URLError('file not on local host')
fo = open(localfile,'rb')
brange = req.headers.get('Range',None)
brange = range_header_to_tuple(brange)
assert brange != ()
if brange:
(fb,lb) = brange
if lb == '': lb = size
if fb < 0 or fb > size or lb > size:
raise RangeError(9, 'Requested Range Not Satisfiable')
size = (lb - fb)
fo = RangeableFileObject(fo, (fb,lb))
headers = mimetools.Message(StringIO(
'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
(mtype or 'text/plain', size, modified)))
return urllib.addinfourl(fo, headers, 'file:'+file)
# FTP Range Support
# Unfortunately, a large amount of base FTP code had to be copied
# from urllib and urllib2 in order to insert the FTP REST command.
# Code modifications for range support have been commented as
# follows:
# -- range support modifications start/end here