本文整理汇总了Python中urllib2.html方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.html方法的具体用法?Python urllib2.html怎么用?Python urllib2.html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.html方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_http_doubleslash
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import html [as 别名]
def test_http_doubleslash(self):
# Checks that the presence of an unnecessary double slash in a url doesn't break anything
# Previously, a double slash directly after the host could cause incorrect parsing of the url
h = urllib2.AbstractHTTPHandler()
o = h.parent = MockOpener()
data = ""
ds_urls = [
"http://example.com/foo/bar/baz.html",
"http://example.com//foo/bar/baz.html",
"http://example.com/foo//bar/baz.html",
"http://example.com/foo/bar//baz.html",
]
for ds_url in ds_urls:
ds_req = Request(ds_url, data)
# Check whether host is determined correctly if there is no proxy
np_ds_req = h.do_request_(ds_req)
self.assertEqual(np_ds_req.unredirected_hdrs["Host"],"example.com")
# Check whether host is determined correctly if there is a proxy
ds_req.set_proxy("someproxy:3128",None)
p_ds_req = h.do_request_(ds_req)
self.assertEqual(p_ds_req.unredirected_hdrs["Host"],"example.com")
示例2: test_invalid_redirect
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import html [as 别名]
def test_invalid_redirect(self):
from_url = "http://example.com/a.html"
valid_schemes = ['http', 'https', 'ftp']
invalid_schemes = ['file', 'imap', 'ldap']
schemeless_url = "example.com/b.html"
h = urllib2.HTTPRedirectHandler()
o = h.parent = MockOpener()
req = Request(from_url)
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
for scheme in invalid_schemes:
invalid_url = scheme + '://' + schemeless_url
self.assertRaises(urllib2.HTTPError, h.http_error_302,
req, MockFile(), 302, "Security Loophole",
MockHeaders({"location": invalid_url}))
for scheme in valid_schemes:
valid_url = scheme + '://' + schemeless_url
h.http_error_302(req, MockFile(), 302, "That's fine",
MockHeaders({"location": valid_url}))
self.assertEqual(o.req.get_full_url(), valid_url)
示例3: new_document
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import html [as 别名]
def new_document(self, content, format="html", title=None, member_ids=[]):
"""Creates a new document from the given content.
To create a document in a folder, include the folder ID in the list
of member_ids, e.g.,
client = quip.QuipClient(...)
user = client.get_authenticated_user()
client.new_document(..., member_ids=[user["archive_folder_id"]])
"""
return self._fetch_json("threads/new-document", post_data={
"content": content,
"format": format,
"title": title,
"member_ids": ",".join(member_ids),
})
示例4: edit_document
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import html [as 别名]
def edit_document(self, thread_id, content, operation=APPEND, format="html",
section_id=None, **kwargs):
"""Edits the given document, adding the given content.
`operation` should be one of the constants described above. If
`operation` is relative to another section of the document, you must
also specify the `section_id`.
"""
# Since our cell ids in 10x contain ';', which is a valid cgi
# parameter separator, we are replacing them with '_' in 10x cell
# sections. This should be no op for all other sections.
section_id = None if not section_id else section_id.replace(";", "_")
args = {
"thread_id": thread_id,
"content": content,
"location": operation,
"format": format,
"section_id": section_id
}
args.update(kwargs)
return self._fetch_json("threads/edit-document", post_data=args)
示例5: get_blob
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import html [as 别名]
def get_blob(self, thread_id, blob_id):
"""Returns a file-like object with the contents of the given blob from
the given thread.
The object is described in detail here:
https://docs.python.org/2/library/urllib2.html#urllib2.urlopen
"""
request = Request(
url=self._url("blob/%s/%s" % (thread_id, blob_id)))
if self.access_token:
request.add_header("Authorization", "Bearer " + self.access_token)
try:
return urlopen(request, timeout=self.request_timeout)
except HTTPError as error:
try:
# Extract the developer-friendly error message from the response
message = json.loads(error.read().decode())["error_description"]
except Exception:
raise error
raise QuipError(error.code, message, error)