本文整理汇总了Python中urllib2.HTTPHandler方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.HTTPHandler方法的具体用法?Python urllib2.HTTPHandler怎么用?Python urllib2.HTTPHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reverseip
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def reverseip(url):
"""return domains from given the same server"""
# get only domain name
url = urlparse(url).netloc if urlparse(url).netloc != '' else urlparse(url).path.split("/")[0]
source = "http://domains.yougetsignal.com/domains.php"
useragent = useragents.get()
contenttype = "application/x-www-form-urlencoded; charset=UTF-8"
# POST method
opener = urllib2.build_opener(
urllib2.HTTPHandler(), urllib2.HTTPSHandler())
data = urllib.urlencode([('remoteAddress', url), ('key', '')])
request = urllib2.Request(source, data)
request.add_header("Content-type", contenttype)
request.add_header("User-Agent", useragent)
try:
result = urllib2.urlopen(request).read()
except urllib2.HTTPError, e:
print >> sys.stderr, "[{}] HTTP error".format(e.code)
示例2: scan
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def scan(url, redirect, insecure, useragent, postdata, proxy):
request = urllib2.Request(url.geturl())
request.add_header('User-Agent', useragent)
request.add_header('Origin', 'http://hsecscan.com')
request.add_header('Accept', '*/*')
if postdata:
request.add_data(urllib.urlencode(postdata))
build = [urllib2.HTTPHandler()]
if redirect:
build.append(RedirectHandler())
if proxy:
build.append(urllib2.ProxyHandler({'http': proxy, 'https': proxy}))
if insecure:
context = ssl._create_unverified_context()
build.append(urllib2.HTTPSHandler(context=context))
urllib2.install_opener(urllib2.build_opener(*build))
response = urllib2.urlopen(request)
print '>> RESPONSE INFO <<'
print_response(response.geturl(), response.getcode(), response.info())
print '>> RESPONSE HEADERS DETAILS <<'
for header in response.info().items():
check_header(header)
print '>> RESPONSE MISSING HEADERS <<'
missing_headers(response.info().items(), url.scheme)
示例3: read_openload
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def read_openload(url):
default_headers = dict()
default_headers[
"User-Agent"] = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3163.100 Safari/537.36"
default_headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
default_headers["Accept-Language"] = "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3"
default_headers["Accept-Charset"] = "UTF-8"
default_headers["Accept-Encoding"] = "gzip"
cj = cookielib.MozillaCookieJar()
request_headers = default_headers.copy()
url = urllib.quote(url, safe="%/:=&?~#+!$,;'@()*[]")
handlers = [urllib2.HTTPHandler(debuglevel=False)]
handlers.append(NoRedirectHandler())
handlers.append(urllib2.HTTPCookieProcessor(cj))
opener = urllib2.build_opener(*handlers)
req = urllib2.Request(url, None, request_headers)
handle = opener.open(req, timeout=None)
return handle.headers.dict.get('location')
示例4: send_report
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def send_report(self, payload=None):
if not payload:
self.logger.debug('Timer triggered report')
if self.msg_stack:
payload = self.msg_stack.pop(-1)
self.logger.debug('Timer triggered report')
else:
self.logger.debug('No more messages to send. Time stopped')
self.timer.stop()
return
handler = urllib2.HTTPHandler()
opener = urllib2.build_opener(handler)
data = urllib.urlencode(payload)
request = urllib2.Request(self.MAIL_URL, data=data)
request.get_method = lambda: "POST"
try:
connection = opener.open(request)
except urllib2.HTTPError, e:
connection = e
示例5: sendResponse
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def sendResponse(event, context, responseStatus, responseData):
responseBody = json.dumps({
"Status": responseStatus,
"Reason": "See the details in CloudWatch Log Stream: " + context.log_stream_name,
"PhysicalResourceId": context.log_stream_name,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"Data": responseData
})
logger.info('ResponseURL: {}'.format(event['ResponseURL']))
logger.info('ResponseBody: {}'.format(responseBody))
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=responseBody)
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(responseBody))
request.get_method = lambda: 'PUT'
response = opener.open(request)
print("Status code: {}".format(response.getcode()))
print("Status message: {}".format(response.msg))
示例6: sendResponse
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def sendResponse(event, context, responseStatus, responseData):
responseBody = json.dumps({
"Status": responseStatus,
"Reason": "See the details in CloudWatch Log Stream: " + context.log_stream_name,
"PhysicalResourceId": context.log_stream_name,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"Data": responseData
})
logger.info('ResponseURL: {}'.format(event['ResponseURL']))
logger.info('ResponseBody: {}'.format(responseBody))
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=responseBody)
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(responseBody))
request.get_method = lambda: 'PUT'
response = opener.open(request)
print("Status code: {}".format(response.getcode()))
print("Status message: {}".format(response.msg))
示例7: download_vcpython27
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def download_vcpython27(self):
"""
Download vcpython27 since some Windows 7 boxes have it and some don't.
:return: None
"""
self._prepare_for_download()
logger.info('Beginning download of vcpython27... this may take a few minutes...')
with open(os.path.join(DOWNLOADS_DIR, 'vcpython27.msi'), 'wb') as f:
if self.PROXY is not None:
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'http': self.PROXY, 'https': self.PROXY})
)
urllib2.install_opener(opener)
f.write(urllib2.urlopen(self.VCPYTHON27_DOWNLOAD_URL, timeout=self.DOWNLOAD_TIMEOUT).read())
logger.debug('Download of vcpython27 complete')
示例8: download_python
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def download_python(self):
"""
Download Python
:return: None
"""
self._prepare_for_download()
logger.info('Beginning download of python')
with open(os.path.join(DOWNLOADS_DIR, 'python-installer.msi'), 'wb') as f:
if self.PROXY is not None:
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'http': self.PROXY, 'https': self.PROXY})
)
urllib2.install_opener(opener)
f.write(urllib2.urlopen(self.PYTHON_DOWNLOAD_URL, timeout=self.DOWNLOAD_TIMEOUT).read())
logger.debug('Download of python complete')
示例9: login
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def login():
cookie_filename = "cookies.txt"
cookiejar = cookielib.MozillaCookieJar(cookie_filename)
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),urllib2.HTTPHandler(debuglevel=0),urllib2.HTTPSHandler(debuglevel=0),urllib2.HTTPCookieProcessor(cookiejar))
page = loadPage(opener, "https://www.linkedin.com/")
parse = BeautifulSoup(page, "html.parser")
csrf = parse.find(id="loginCsrfParam-login")['value']
login_data = urllib.urlencode({'session_key': username, 'session_password': password, 'loginCsrfParam': csrf})
page = loadPage(opener,"https://www.linkedin.com/uas/login-submit", login_data)
parse = BeautifulSoup(page, "html.parser")
cookie = ""
try:
cookie = cookiejar._cookies['.www.linkedin.com']['/']['li_at'].value
except:
sys.exit(0)
cookiejar.save()
os.remove(cookie_filename)
return cookie
示例10: check_gn_proxy
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def check_gn_proxy(proxy, protocal_type='HTTP'):
url = 'http://icanhazip.com'
proxy_handler = urllib2.ProxyHandler({
'http': 'http://' + proxy,
'https': 'https://' + proxy,
})
if protocal_type == 'HTTPS':
url = 'https://icanhazip.com'
opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
try:
response = opener.open(url, timeout=3)
res_ip = response.read().strip()
return response.code == 200 and res_ip == proxy.split(':')[0]
except Exception:
return False
示例11: error_handler
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def error_handler(url):
global HANDLE_ERRORS
orig = HANDLE_ERRORS
keepalive_handler = HTTPHandler()
opener = urllib2.build_opener(keepalive_handler)
urllib2.install_opener(opener)
pos = {0: 'off', 1: 'on'}
for i in (0, 1):
print " fancy error handling %s (HANDLE_ERRORS = %i)" % (pos[i], i)
HANDLE_ERRORS = i
try:
fo = urllib2.urlopen(url)
foo = fo.read()
fo.close()
try: status, reason = fo.status, fo.reason
except AttributeError: status, reason = None, None
except IOError, e:
print " EXCEPTION: %s" % e
raise
else:
print " status = %s, reason = %s" % (status, reason)
示例12: comp
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def comp(N, url):
print ' making %i connections to:\n %s' % (N, url)
sys.stdout.write(' first using the normal urllib handlers')
# first use normal opener
opener = urllib2.build_opener()
urllib2.install_opener(opener)
t1 = fetch(N, url)
print ' TIME: %.3f s' % t1
sys.stdout.write(' now using the keepalive handler ')
# now install the keepalive handler and try again
opener = urllib2.build_opener(HTTPHandler())
urllib2.install_opener(opener)
t2 = fetch(N, url)
print ' TIME: %.3f s' % t2
print ' improvement factor: %.2f' % (t1/t2, )
示例13: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def __init__(self, configuration):
self.setup(configuration)
self.echo = None
if "ECHO" in configuration:
self.echo = configuration['ECHO']
if self.proxy_scheme is not None and self.proxy_host is not None and \
self.proxy_port is not None:
credentials = ""
if self.proxy_username is not None and self.proxy_password is not None:
credentials = self.proxy_username + ":" + self.proxy_password + "@"
proxyDict = {
self.proxy_scheme: self.proxy_scheme + "://" + credentials +
self.proxy_host + ":" + self.proxy_port
}
proxy = urllib2.ProxyHandler(proxyDict)
if credentials != '':
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
else:
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
示例14: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def __init__(self, *args, **kwargs):
self.args = args
self.kw = kwargs
urllib2.HTTPHandler.__init__(self)
示例15: _GetOpener
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPHandler [as 别名]
def _GetOpener(self):
"""Returns an OpenerDirector that supports cookies and ignores redirects.
Returns:
A urllib2.OpenerDirector object.
"""
opener = urllib2.OpenerDirector()
opener.add_handler(fancy_urllib.FancyProxyHandler())
opener.add_handler(urllib2.UnknownHandler())
opener.add_handler(urllib2.HTTPHandler())
opener.add_handler(urllib2.HTTPDefaultErrorHandler())
opener.add_handler(fancy_urllib.FancyHTTPSHandler())
opener.add_handler(urllib2.HTTPErrorProcessor())
opener.add_handler(ContentEncodingHandler())
if self.save_cookies:
self.cookie_jar.filename = os.path.expanduser(
HttpRpcServer.DEFAULT_COOKIE_FILE_PATH)
if os.path.exists(self.cookie_jar.filename):
try:
self.cookie_jar.load()
self.authenticated = True
logger.debug("Loaded authentication cookies from %s",
self.cookie_jar.filename)
except (OSError, IOError, cookielib.LoadError), e:
logger.debug("Could not load authentication cookies; %s: %s",
e.__class__.__name__, e)
self.cookie_jar.filename = None
else:
try:
fd = os.open(self.cookie_jar.filename, os.O_CREAT, 0600)
os.close(fd)
except (OSError, IOError), e:
logger.debug("Could not create authentication cookies file; %s: %s",
e.__class__.__name__, e)
self.cookie_jar.filename = None