python: urllib.request.urlopen() HTTP Error 308 Permanent redirect - python

i was just trying to read a file from the internet..
then an error came..
Traceback (most recent call last):
File "E:\Sapphire\Programming\download_file.py", line 46, in <module>
download(arg[1])
File "E:\Sapphire\Programming\download_file.py", line 19, in download
file_info = req.urlopen("http://proget.whirlpool.repl.co/{}/{}.txt".format(arch, name))
File "E:\Programming\Python38-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "E:\Programming\Python38-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "E:\Programming\Python38-32\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "E:\Programming\Python38-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "E:\Programming\Python38-32\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "E:\Programming\Python38-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 308: Permanent Redirect
the line that brought the error (line 20):
file_info = req.urlopen("http://proget.whirlpool.repl.co/{}/{}.txt".format(arch, name))
(the link that it should use is correct..)
any ideas how it came?
SPECS:
OS: Windows 7 SP1
Python: Python 3.8.10
Arch: x86 (32-bit)

Http is outdated, so most sites would redirect to https.
So use
file_info = req.urlopen("https://proget.whirlpool.repl.co/{}/{}.txt".format(arch, name))

Related

downloading a CSV with wget

Hi i am newish to python, and working on modeling of the CV-19 outbreak in the UK.
Currently I am writting a program to automatically download the UK governments latest death statistics.
Here is my code so far:
import wget
url = "https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv"#
wget.download(url, 'C:/Users/Moshe/Downloads/Covid_19_uk_timeseries.csv')
Running this in pycharm I get:
C:\Users\Moshe\PycharmProjects\HelloWorld\venv\Scripts\python.exe C:/Users/Moshe/.PyCharmCE2019.1/config/scratches/Get_CV19_UK_stats.py
Traceback (most recent call last):
File "C:/Users/Moshe/.PyCharmCE2019.1/config/scratches/Get_CV19_UK_stats.py", line 6, in <module>
wget.download(url, 'C:/Users/Moshe/Downloads/Covid_19_uk_timeseries.csv')
File "C:\Users\Moshe\PycharmProjects\HelloWorld\venv\lib\site-packages\wget.py", line 526, in download
(tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 308: Permanent Redirect
Process finished with exit code 1
The file I am trying to get can be found on this page:
https://coronavirus.data.gov.uk/?_ga=2.157835066.1251021075.1589887735-1783596499.1585566366
Why won't this work? and what do i need to change?
Can you verify that your output path is valid and writeable?
try
import wget
import os
output_dir = 'C:/Users/Moshe/Downloads'
output_file = 'Covid_19_uk_timeseries.csv'
assert os.path.exists(output_dir)
url = "https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv"#
wget.download(url, out = os.path.join(output_dir,output_file))

pytube urllib.error.HTTPError: HTTP Error 403: Forbidden

I get an error when i try to download a video from youtube with pytube.
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=9bZkp7q19f0')
stream = yt.streams.first()
stream
stream.download()
The error says "Forbidden", but fetching e.g. with curl from the same URL seems to work just fine. What could be wrong here?
Traceback (most recent call last):
File "C:/Users/Mr. jarvis/Desktop/youtube/youtube.py", line 7, in <module>
stream.download()
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\streams.py", line 217, in download
bytes_remaining = self.filesize
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\streams.py", line 164, in filesize
headers = request.get(self.url, headers=True)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\request.py", line 21, in get
response = urlopen(url)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Use pytube3 instead, it fixes this error (but is Python 3 only): https://github.com/hbmartin/pytube3
Experienced the same problem. I found unfinished downloaded file in the project folder. After deleting the unfinished downloaded file and running stream.download() again, it worked. So maybe worth checking.
pytube version 12.0.0

Getting 504 error when trying to parse some text with tika in python

Some weeks ago I had tika-python working without any issue in Windows 10. Today I had to re-create my virtualenv and upgraded tika to version 1.19 but when I tried to use it as usual and I got 502 and 504 errors all the time.
I tried to use it in ubuntu18.04 and with previous tika versions and nothing changed.
Can anyone help?
(I'm not a native english speaker so sorry if my english is not very good)
parsed_data = parser.from_buffer(buffer)
2019-05-25 20:40:42,446 [MainThread ] [INFO ] Retrieving http://search.maven.org/remotecontent?filepath=org/apache/tika/tika-server/1.15/tika-server-1.15.jar.md5 to /tmp/tika-server.jar.md5.
Traceback (most recent call last):
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/tika.py", line 651, in getRemoteJar
urlretrieve(urlOrPath, destPath)
File "/usr/lib/python3.7/urllib/request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 504: Gateway Time-out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/parser.py", line 51, in from_buffer
{'Accept': 'application/json'}, False)
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/tika.py", line 506, in callServer
serverEndpoint = checkTikaServer(scheme, serverHost, port, tikaServerJar, classpath)
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/tika.py", line 557, in checkTikaServer
if not checkJarSig(tikaServerJar, jarPath):
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/tika.py", line 572, in checkJarSig
getRemoteJar(tikaServerJar + ".md5", jarPath + ".md5")
File "/home/ohm/Documentos/TFG/venv/lib/python3.7/site-packages/tika/tika.py", line 661, in getRemoteJar
urlretrieve(urlOrPath, destPath)
File "/usr/lib/python3.7/urllib/request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPErr: HTTP Error 502: Bad Gateway

Jenkins Python API, create_node(), error: 400

I'm setting my jenkins environment and, as part of this process, I need to create a slave node.
Bellow follow the script that is crashing:
import jenkins
server = jenkins.Jenkins('http://localhost:9090')
server.create_node('slave')
Follows the output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/jenkins/__init__.py", line 1156, in create_node
self._build_url(CREATE_NODE, params), b''))
File "/usr/local/lib/python2.7/dist-packages/jenkins/__init__.py", line 341, in jenkins_open
response = urlopen(req, timeout=self.timeout).read()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Use jenkinsapi module,
from jenkinsapi.jenkins import Jenkins
server = Jenkins('http://localhost:9090')
server.create_node('slave')
It appears to be a bug in python-jenkins 0.4.11, which was fixed by commit
302eb5dad6439842e076fead5bd76f20f615e6a8.
I got a similar error, upgrading python-jenkins fixed it.

Linux curl -F equavilant command in python

I need to write the equivalent code of linux command in my python program command is
curl -F file=#/path/to/file http://localhost:8090/tasks/create/file
I have translated this command into python using urllib as
import urllib
import urllib2
url='http://localhost:8090/tasks/create/file'
values={'file' : '#/home/hacker/sample/00.EXE'}
data=urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
but this does not do the required work and following error is printed on python console
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib64/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
while on the server console following error is given
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/lib/python2.6/site-packages/bottle.py", line 1729, in wrapper
rv = callback(*a, **ka)
File "./api.py", line 69, in tasks_create_file
temp_file_path = store_temp_file(data.file.read(), data.filename)
AttributeError: 'unicode' object has no attribute 'file'
127.0.0.1 - - [06/Aug/2014 11:53:01] "POST /tasks/create/file HTTP/1.1" 500 758

Categories