python-2.7 - wget 和 urllib 无法从 python 3 上的 url 加载文件

标签 python-2.7 python-3.x gps wget urllib

我正在尝试下载给定 url 和端口的 txt 文件。这适用于 python 2 这样做:

Python 2.7.12 (default, Sep 26 2016, 09:46:23)  [GCC 4.2.1 Compatible
Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright",
"credits" or "license" for more information.
>>> import urllib
>>> foo = urllib.urlopen("http://catnet-ip.icc.cat:8080/")
>>> foo.read() 
'SOURCETABLE 200 OK\r\nServer: NTRIP Trimble NTRIP Caster\r\nContent-Type: text/plain\r\nContent-Length: 2884\r\nDate:
02/Nov/2016:12:52:19 UTC\r\n\r\nSTR;VRS_RTK_2_3;Virtual RTK ver RTCM
2.3;RTCM 2.3;1(1),3(6),18(1),19(1),23(5),24(5);2;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;3900;;\r\nSTR;VRS_RTK_3_0;Virtual RTK ver RTCM
3.0;RTCM 3;1004(1),1005/1007(5),PBS(10);2;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;1100;;\r\nSTR;VRS_DGPS;Virtual DGPS ver RTCM 2.3;RTCM
2.3;1(1),3(6),22(6),23/24(5),16(59);0;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;640;;\r\n 
...

与 wget 类似:

Python 2.7.12 (default, Sep 26 2016, 09:46:23)  [GCC 4.2.1 Compatible
Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright",
"credits" or "license" for more information.
>>> import wget
>>> foo = wget.download("http://catnet-ip.icc.cat:8080/", bar=None)
>>> foo
>>> ' (1).'
>>> exit()
$ less \ \(1\).
SOURCETABLE 200 OK\r\nServer: NTRIP Trimble NTRIP Caster\r\nContent-Type: text/plain\r\nContent-Length: 2884\r\nDate:
02/Nov/2016:12:52:19 UTC\r\n\r\nSTR;VRS_RTK_2_3;Virtual RTK ver RTCM
2.3;RTCM 2.3;1(1),3(6),18(1),19(1),23(5),24(5);2;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;3900;;\r\nSTR;VRS_RTK_3_0;Virtual RTK ver RTCM
3.0;RTCM 3;1004(1),1005/1007(5),PBS(10);2;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;1100;;\r\nSTR;VRS_DGPS;Virtual DGPS ver RTCM 2.3;RTCM
2.3;1(1),3(6),22(6),23/24(5),16(59);0;GPS;Catnet;ESP;41.3;2.09;1;1;Trimble
GPSNet;None;B;N;640;;\r\n 
...

但两者都在 python 3 上失败,并出现错误“http.client.BadStatusLine: SOURCETABLE 200 OK”

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> foo = urllib.request.urlopen("http://catnet-ip.icc.cat:8080/") 
Traceback (most recent call last):   
File "<stdin>", line 1, in <module>       
File /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)   
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1257, in do_open
    r = h.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 279, in _read_status
    raise BadStatusLine(line) 
http.client.BadStatusLine: SOURCETABLE 200 OK

和:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import wget
>>> wget.download("http://catnet-ip.icc.cat:8080/")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/toni/Downloads/wget-2.0/wget.py", line 308, in download
    (tmpfile, headers) = urllib.urlretrieve(url, tmpfile, callback)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 188, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1257, in do_open
    r = h.getresponse()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 279, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: SOURCETABLE 200 OK

来自python docs on http protocol我想这是由于 urllib 和 wget 理解我想要作为一些 http 代码加载的文件的第一个位置中的标签“SOURCETABLE”。此标签始终存在于我要下载的文件 ( ntrip casters ) 中,但我找不到解决该问题的方法。

最佳答案

我在使用不同的 NTRIP 服务器时遇到了同样的问题。根据 RFC 2616,SOURCETABLE 200 OK 不是有效的 HTTP 状态代码。唉。我的解决方法:curl,特别是pycurl

例如:

import sys
import pycurl

def handle_write(buf):
    sys.stdout.write(buf.decode("iso-8859-1"))

host = "http://catnet-ip.icc.cat:8080/"
curl = pycurl.Curl()
curl.setopt(pycurl.URL, host)
curl.setopt(pycurl.TIMEOUT, 20)
curl.setopt(pycurl.CONNECTTIMEOUT, 3)
curl.setopt(pycurl.HEADERFUNCTION, sys.stdout.write)
curl.setopt(pycurl.WRITEFUNCTION, handle_write)

curl.perform()
curl.close()

结果:

SOURCETABLE 200 OK
Server: GNSS Spider 7.4.0.8125/1.0
Date: Wed, 18 Dec 2019 21:29:11 GMT Standard Time
Content-Type: text/plain
Content-Length: 2667

STR;VRS_RTK_3_0;VRS_RTK_3_0;RTCM 
...
ENDSOURCETABLE 

关于python-2.7 - wget 和 urllib 无法从 python 3 上的 url 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381071/

相关文章:

android - 使用 SQLite 和 Android 查找附近的位置

python - 替换字典中的键

python-2.7 - 使用tweepy时出现401错误

python - 有没有办法检查我的代码的哪一部分使文件句柄处于打开状态

android - 转换 GPS 坐标以匹配自定义 2d 户外布局图像

Android Google Map如何检查gps位置是否在圆圈内

python - 根据信息拆分数组

python - 无法在 MacOS Mojave 上的 Python 2.7 上安装 MySQLdb

正则表达式上的 Python TypeError

python - 按组将列转换为行