问题1:requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443):Max retries exceeded with url: /login/ (Caused by SSLError(SSLError("bad handshake:Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed
pip install cryptography
pip install pyOpenSSL
pip install certifi
请求方法里加入 verify=False
问题2:requests.exceptions.ProxyError: HTTPSConnectionPool(host='xxx', port=443): Max retries exceeded with url: xxx (Caused by ProxyError('Cannot connect to proxy.', error('Invalid response from tunnel request',)))
pip install PySocks
pip install pysocks5
proxy = {
'http': 'socks5://xxx.xxx.xxx.xxx:xxxx'
}
具体设置可能跟使用的代理服务有关
错误分为语法错误和逻辑错误
1、语法错误
程序执行过程中,python解释器会检测你的程序是否存在语法错误,如果程序出错python解释器会指出出错的一行,并且在最先找到的,错误的文职标记了一个小小的箭头。
2、逻辑错误
在生活中0是不能作为被除数的,程序写的语法可能没问题,但是逻辑上不一定没有问题,这就是一种逻辑错误。
异常处理方法:
Python内置的try...except...finally用来处理错误十分方便,出错时,会分析错误信息并定位错误发生的代码位置才是最关键的。
程序也可以主动抛出错误,让调用者来处理相应的错误,但是,应该在文档中写清楚可能会抛出哪些错误,以及错误产生的原因。
扩展资料:实例:下面是简单的例子,它打开一个文件,在该文件中的内容写入内容,但文件没有写入权限,发生了异常:
#!/usr/bin/python#-*-coding:UTF-8-*-try:
fh=open("testfile","w")
fh.write("这是一个测试文件,用于测试异常!!")exceptIOError:
print"Error:没有找到文件或读取文件失败"else:
print"内容写入文件成功"
fh.close()
在执行代码前为了测试方便,我们可以先去掉testfile文件的写权限,命令如下:
chmod-wtestfile
再执行以上代码:
$pythontest.py
Error:没有找到文件或读取文件失败
Python3 使用hdfs分布式文件储存系统
from pyhdfs import *
client = HdfsClient(hosts="testhdfs.org, 50070",
user_name="web_crawler") # 创建一个连接
client.get_home_directory() # 获取hdfs根路径
client.listdir(PATH) # 获取hdfs指定路径下的文件列表
client.copy_from_local(file_path, hdfs_path, overwrite=True) # 把本地文件拷贝到服务器,不支持文件夹;overwrite=True表示存在则覆盖
client.delete(PATH, recursive=True) # 删除指定文件
hdfs_path必须包含文件名及其后缀,不然不会成功
如果连接
HdfsClient
报错
Traceback (most recent call last):
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "
client.get_home_directory()
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhdfs.py", line 565, in get_home_directory
return _json(self._get('/', 'GETHOMEDIRECTORY', **kwargs))['Path']
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhdfs.py", line 391, in _get
return self._request('get', *args, **kwargs)
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhdfs.py", line 377, in _request
_check_response(response, expected_status)
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhdfs.py", line 799, in _check_response
remote_exception = _json(response)['RemoteException']
File "C:\Users\billl\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhdfs.py", line 793, in _json
"Expected JSON. Is WebHDFS enabled? Got {!r}".format(response.text))
pyhdfs.HdfsException: Expected JSON. Is WebHDFS enabled? Got '\n\n\n\n
502 Server dropped connection
\n
The following error occurred while trying to access http://%2050070:50070/webhdfs/v1/?user.name=web_crawler&op=GETHOMEDIRECTORY :
\n 502 Server dropped connection
\n
Generated Fri, 21 Dec 2018 02:03:18 GMT by Polipo on .\n\r\n'
则一般是访问认证错误,可能原因是账户密码不正确或者无权限,或者本地网络不在可访问名单中