Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查python中是否存在https网页_Python_Http - Fatal编程技术网

检查python中是否存在https网页

检查python中是否存在https网页,python,http,Python,Http,在Python2.x脚本中,我正在寻找检查https页面是否返回特定内容的功能(可能需要解析页面内容才能发现这一点)。该页面还有一个htpasswd提示符,需要使用用户名和密码进行身份验证才能看到内容。因此,我假设我正在寻找一个模块或其他功能,它为我提供了硬编码用户名和密码的能力,这样它就可以获取页面,我就可以操作输出(也就是检查是否存在表示404页面的等价关键字) 我正在看,但它似乎不符合我的要求 您可以使用httplib模块来实现,但是有一些更简单的方法不需要手动驱动HTTP协议 使用(首先

在Python2.x脚本中,我正在寻找检查https页面是否返回特定内容的功能(可能需要解析页面内容才能发现这一点)。该页面还有一个htpasswd提示符,需要使用用户名和密码进行身份验证才能看到内容。因此,我假设我正在寻找一个模块或其他功能,它为我提供了硬编码用户名和密码的能力,这样它就可以获取页面,我就可以操作输出(也就是检查是否存在表示404页面的等价关键字)

我正在看,但它似乎不符合我的要求

您可以使用
httplib
模块来实现,但是有一些更简单的方法不需要手动驱动HTTP协议

使用(首先需要安装的外部模块)可能是最简单的:

import requests

auth = ('someusername', 'somepassword')
response = requests.get(yoururl, auth=auth)
response.raise_for_status()
如果响应未成功或返回404 Not Found,这将引发异常

然后可以使用
response.content
(字节字符串)或
response.text
(unicode响应)进一步解析响应正文

仅使用标准库,使用

import urllib2, base64

request = urllib2.Request(yoururl)
authstring = base64.encodestring('{}:{}'.format('someusername', 'somepassword')).strip()
request.add_header("Authorization", "Basic {}".format(authstring))   
response = urllib2.urlopen(request)

if not 200 <= response.getcode() < 400:
    # error response, raise an exception here?

content = response.read()
try:
    text = content.decode(response.info().getparam('charset', 'utf8'))
except UnicodeDecodeError:
    text = content.decode('ascii', 'replace')
导入urllib2,base64 request=urlib2.request(您的URL) authstring=base64.encodestring({}:{}.format('someusername','somepassword')).strip() add_头(“授权”,“基本{}”。格式(authstring)) response=urllib2.urlopen(请求) 如果不是200