爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 3659|回复: 2

Python下载grib2资料出问题

[复制链接]

新浪微博达人勋

发表于 2020-9-8 12:03:41 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
Downloading fnl_20200903_00_00.grib2

4.091 % Completed
8.182 % CompletedTraceback (most recent call last):
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\response.py", line 436, in _error_catcher
    yield
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\response.py", line 518, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 454, in read
    n = self.readinto(b)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 498, in readinto
    n = self.fp.readinto(b)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\response.py", line 575, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\response.py", line 540, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\response.py", line 454, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionAbortedError(10053, '您的主机中的软件中止了一个已建立的连接。', None, 10053, None)", ConnectionAbortedError(10053, '您的主机中的软件中止了一个已建立的连接。', None, 10053, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lvdeji\项目\python_work\ncepdownload.py", line 68, in <module>
    for chunk in req.iter_content(chunk_size=chunk_size):
  File "C:\Users\Lvdeji\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\models.py", line 754, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionAbortedError(10053, '您的主机中的软件中止了一个已建立的连接。', None, 10053, None)", ConnectionAbortedError(10053, '您的主机中的软件中止了一个已建立的连接。', None, 10053, None))

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2020-9-8 12:37:41 | 显示全部楼层
#!/usr/bin/env python
#################################################################
# Python Script to retrieve 4 online Data files of 'ds083.2',
# total 101.86M. This script uses 'requests' to download data.
#
# Highlight this script by Select All, Copy and Paste it into a file;
# make the file executable and run it on command line.
#
# You need pass in your password as a parameter to execute
# this script; or you can set an environment variable RDAPSWD
# if your Operating System supports it.
#
# Contact rpconroy@ucar.edu (Riley Conroy) for further assistance.
#################################################################


import sys, os
import requests

def check_file_status(filepath, filesize):
    sys.stdout.write('\r')
    sys.stdout.flush()
    size = int(os.stat(filepath).st_size)
    percent_complete = (size/filesize)*100
    sys.stdout.write('%.3f %s' % (percent_complete, '% Completed'))
    sys.stdout.flush()

# Try to get password
if len(sys.argv) < 2 and not 'RDAPSWD' in os.environ:
    try:
        import getpass
        input = getpass.getpass
    except:
        try:
            input = raw_input
        except:
            pass
    pswd = '********'
else:
    try:
        pswd = sys.argv[1]
    except:
        pswd = os.environ['RDAPSWD']

url = 'https://rda.ucar.edu/cgi-bin/login'
values = {'email' : '*******', 'passwd' : pswd, 'action' : 'login'}
# Authenticate
ret = requests.post(url,data=values)
if ret.status_code != 200:
    print('Bad Authentication')
    print(ret.text)
    exit(1)
dspath = 'https://rda.ucar.edu/data/ds083.2/'
filelist=[
'grib2/2020/2020.09/fnl_20200903_00_00.grib2',
'grib2/2020/2020.09/fnl_20200903_06_00.grib2',
'grib2/2020/2020.09/fnl_20200903_12_00.grib2',
'grib2/2020/2020.09/fnl_20200903_18_00.grib2',
]
for file in filelist:
    filename=dspath+file
    file_base = os.path.basename(file)
    print('Downloading',file_base)
    req = requests.get(filename, cookies = ret.cookies, allow_redirects=True, stream=True)
    filesize = int(req.headers['Content-length'])
    with open(file_base, 'wb') as outfile:
        chunk_size=1048576
        for chunk in req.iter_content(chunk_size=chunk_size):
            outfile.write(chunk)
            if chunk_size < filesize:
                check_file_status(file_base, filesize)
    check_file_status(file_base, filesize)
    print()
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-9-8 12:48:46 | 显示全部楼层
下面的是程序,上面是Python运行错误,求大神指导
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表