"""Open an arbitrary URL.
See the following document for more info on URLs:
"Names and Addresses, URIs, URLs, URNs, URCs", at
http://www.w3.org/pub/WWW/Addressing/Overview.html
See also the HTTP spec (from which the error codes are derived):
"HTTP - Hypertext Transfer Protocol", at
http://www.w3.org/pub/WWW/Protocols/
Related standards and specs:
- RFC1808: the "relative URL" spec. (authoritative status)
- RFC1738 - the "URL standard". (authoritative status)
- RFC1630 - the "URI spec". (informational status)
The object returned by URLopener().open(file) will differ per
protocol. All you know is that is has methods read(), readline(),
readlines(), fileno(), close() and info(). The read*(), fileno()
and close() methods work like those of open files.
The info() method returns a mimetools.Message object which can be
used to query various info about the object, if available.
(mimetools.Message objects are queried with the getheader() method.)
"""
import string
import socket
import os
import sys
import types
__all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve",
"urlcleanup", "quote", "quote_plus", "unquote", "unquote_plus",
"urlencode", "url2pathname", "pathname2url", "splittag",
"localhost", "thishost", "ftperrors", "basejoin", "unwrap",
"splittype", "splithost", "splituser", "splitpasswd", "splitport",
"splitnport", "splitquery", "splitattr", "splitvalue",
"splitgophertype", "getproxies"]
__version__ = '1.15' # XXX This version is not always updated :-(
MAXFTPCACHE = 10 # Trim the ftp cache beyond this size
# Helper for non-unix systems
if os.name == 'mac':
from macurl2path import url2pathname, pathname2url
elif os.name == 'nt':
from nturl2path import url2pathname, pathname2url
elif os.name == 'riscos':
from rourl2path import url2pathname, pathname2url
else:
def url2pathname(pathname):
return unquote(pathname)
def pathname2url(pathname):
return quote(pathname)
# This really consists of two pieces:
# (1) a class which handles opening of all sorts of URLs
# (plus assorted utilities etc.)
# (2) a set of functions for parsing URLs
# XXX Should these be separated out into different modules?
# Shortcut for basic usage
_urlopener = None
def urlopen(url, data=None):
"""urlopen(url [, data]) -> open file-like object"""
global _urlopener
if not _urlopener:
_urlopener = FancyURLopener()
if data is None:
return _urlopener.open(url)
else:
return _urlopener.open(url, data)
def urlretrieve(url, filename=None, reporthook=None, data=None):
global _urlopener
if not _urlopener:
_urlopener = FancyURLopener()
return _urlopener.retrieve(url, filename, reporthook, data)
def urlcleanup():
if _urlopener:
_urlopener.cleanup()
ftpcache = {}
class URLopener:
"""Class to open URLs.
This is a class rather than just a subroutine because we may need
more than one set of global protocol-specific options.
Note -- this is a base class for those who don't want the
automatic handling of errors type 302 (relocated) and 401
(authorization needed)."""
__tempfiles = None
version = "Python-urllib/%s" % __version__
# Constructor
def __init__(self, proxies=None, **x509):
if proxies is None:
proxies = getproxies()
assert hasattr(proxies, 'has_key'), "proxies must be a mapping"
self.proxies = proxies
self.key_file = x509.get('key_file')
self.cert_file = x509.get('cert_file')
self.addheaders = [('User-agent', self.version)]
self.__tempfiles = []
self.__unlink = os.unlink # See cleanup()
self.tempcache = None
# Undocumented feature: if you assign {} to tempcache,
# it is used to cache files retrieved with
# self.retrieve(). This is not enabled by default
# since it does not work for changing documents (and I
# haven't got the logic to check expiration headers
# yet).
self.ftpcache = ftpcache
# Undocumented feature: you can use a different
# ftp cache by assigning to the .ftpcache member;
# in case you want logically independent URL openers
# XXX This is not threadsafe. Bah.
def __del__(self):
self.close()
def close(self):
self.cleanup()
def cleanup(self):
# This code sometimes runs when the rest of this module
# has already been deleted, so it can't use any globals
# or import anything.
if self.__tempfiles:
for file in self.__tempfiles:
try:
self.__unlink(file)
except:
pass
del self.__tempfiles[:]
if self.tempcache:
self.tempcache.clear()
def addheader(self, *args):
"""Add a header to be used by the HTTP interface only
e.g. u.addheader('Accept', 'sound/basic')"""
self.addheaders.append(args)
# External interface
def open(self, fullurl, data=None):
"""Use URLopener().open(file) instead of open(file, 'r')."""
fullurl = unwrap(toBytes(fullurl))
if self.tempcache and self.tempcache.has_key(fullurl):
filename, headers = self.tempcache[fullurl]
fp = open(filename, 'rb')
return addinfourl(fp, headers, fullurl)
urltype, url = splittype(fullurl)
if not urltype:
urltype = 'file'
if self.proxies.has_key(urltype):
proxy = self.proxies[urltype]
urltype, proxyhost = splittype(proxy)
host, selector = splithost(proxyhost)
url = (host, fullurl) # Signal special case to open_*()
else:
proxy = None
name = 'open_' + urltype
self.type = urltype
if '-' in name:
# replace - with _
name = '_'.join(name.split('-'))
if not hasattr(self, name):
if proxy:
return self.open_unknown_proxy(proxy, fullurl, data)
else:
return self.open_unknown(fullurl, data)
try:
if data is None:
return getattr(self, name)(url)
else:
return getattr(self, name)(url, data)
except socket.error, msg:
raise IOError, ('socket error', msg), sys.exc_info()[2]
def open_unknown(self, fullurl, data=None):
"""Overridable interface to open unknown URL type."""
type, url = splittype(fullurl)
raise IOError, ('url error', 'unknown url type', type)
def open_unknown_proxy(self, proxy, fullurl, data=None):
"""Overridable interface to open unknown URL type."""
type, url = splittype(fullurl)
raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
# External interface
def retrieve(self, url, filename=None, reporthook=None, data=None):
"""retrieve(url) returns (filename, None) for a local object
or (tempfilename, headers) for a remote object."""
url = unwrap(toBytes(url))
if self.tempcache and self.tempcache.has_key(url):
return self.tempcache[url]
type, url1 = splittype(url)
if not filename and (not type or type == 'file'):
try:
fp = self.open_local_file(url1)
hdrs = fp.info()
del fp
return url2pathname(splithost(url1)[1]), hdrs
except IOError, msg:
pass
fp = self.open(url, data)
headers = fp.info()
if not filename:
import tempfile
garbage, path = splittype(url)
garbage,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python 3.0 eclipse plugin (614个子文件)
AUTHORS 882B
ChangeLog 68KB
COPYING 2KB
icon_no_border.cpt 98KB
_ctypes.dll 281KB
.eclipseextension 60B
icon128_1.gif 3KB
icon128_2.GIF 3KB
icon128_1_no_borders.gif 2KB
python-big.gif 2KB
icon32_2.gif 1KB
Pydev.gif 1KB
icon32_1.gif 1KB
mainfunction.gif 986B
sample.gif 983B
icon16_2.gif 963B
python_coverage.gif 960B
python_unit.gif 958B
jython_unit.gif 952B
python.gif 940B
icon16_1.gif 935B
jython.gif 932B
imp_rel_obj.gif 857B
pythonNature.gif 835B
reddot.GIF 832B
packagefolder_obj.gif 709B
save.gif 639B
jar_desc_obj.gif 618B
showerr_tsk.gif 605B
jar_src_obj.gif 596B
jar_lsrc_obj.gif 589B
jar_obj.gif 587B
home_nav.gif 582B
jar_l_obj.gif 581B
python.gif 571B
fields_co.gif 554B
testok.gif 385B
project_source_folder.gif 379B
testerr.gif 379B
testfail.gif 378B
refresh_nav.gif 368B
methpub_obj.gif 364B
methpub_obj.gif 364B
methpub_static_obj.gif 355B
methpub_class_obj.gif 353B
remove.gif 351B
annotation_obj.gif 350B
jar_nonexist_obj.gif 341B
breakmarker_gray_conditional.gif 339B
library_obj.gif 338B
breakmarker_conditional.gif 337B
backward_nav.gif 327B
breakmarker.gif 327B
breakmarker_gray.gif 322B
add_correction.gif 318B
class_protected_obj.gif 241B
class_private_obj.gif 240B
package_obj.gif 227B
alphab_sort_co.gif 224B
junit.gif 221B
filter.gif 219B
correction_move.gif 217B
terminate.gif 215B
forward_nav.gif 215B
static_co.gif 213B
magic_co.gif 201B
methmag_static_obj.gif 194B
methmag_class_obj.gif 193B
methmag_obj.gif 176B
class_obj.gif 161B
class_obj.gif 161B
folder.gif 160B
sync_ed.gif 160B
template.gif 155B
file.gif 152B
pydev_package_explorer.gif 145B
parameters_obj.gif 136B
copy.gif 125B
watch_exp.gif 125B
field_public_obj.gif 124B
field_protected_obj.gif 119B
field_default_obj.gif 118B
testsuccess_ovr.gif 114B
greendot.gif 113B
collapseall.gif 112B
methpri_static_obj.gif 112B
methpri_class_obj.gif 111B
methpro_static_obj.gif 109B
arguments.gif 108B
methpro_class_obj.gif 107B
expand.gif 105B
attrpub_obj.gif 97B
impc_obj.gif 97B
attrpub_obj.gif 97B
greendot_big.gif 97B
methpri_obj.gif 94B
methpro_obj.gif 91B
imp_obj.gif 88B
field_private_obj.gif 87B
builtin_obj.gif 87B
共 614 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- Pig/xiaofa2013-11-30有用,谢啦!
pcjavanet
- 粉丝: 92
- 资源: 97
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功