我正在尝试下载一个txt文件,给出了一个url和端口。这在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
型
从http协议的python文档中,我猜这是由于urllib和wget理解了我想作为http代码加载的文件的第一个位置的标签“SOURCETABLE”。这个标签总是出现在我想下载的文件中(ntrip casters),但我找不到解决这个问题的方法。
3条答案
按热度按时间camsedfj1#
我在另一个NTRIP服务器上遇到了这个问题。根据RFC 2616,
SOURCETABLE 200 OK
不是有效的HTTP状态码。唉。我的解决办法是:curl
,特别是pycurl
。举例来说:
字符串
结果:
型
iovurdzv2#
1.酒店Via Urllib 3
字符串
1.通过urllib按照下一个方法编辑文件,然后尝试以下操作
型
1.通过http模块您可以修改python http库,以额外的状态码,您需要在
{python programm folder}/lib/http/client.py
中编辑文件查找代码行
型
修改以
型
以下行的另一个修改
型
修改为
型
现在你可以使用下面的代码在python控制台上获取源表,但是你需要编辑更多的文件来支持NTRIP。
型
wpcxdonn3#
另一种方法是使用标准库中的原始
socket
s:字符串
现在你可以自己解析
resp_payload
,或者让http.client.HTTPResponse
像上面提到的here那样解析,但是需要做一些修改来克服SOURCETABLE
状态行:型