So, I want to download a binary file(encrypted with gpg) using urllib. Now if I would pipe it:
this_script.py > file1
I should be able to decrypt the binary file1 but, the gpg just gives an error. I don't want to save the file in python, as I will need it for my program which decrypts downloaded file. Also I cannot use urlretireve as I can't spoof the useragent string, which causes the site to give me 403 Forbidden Error.
import urllib
url = <link_to_any_binary_file>
req = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0'
}
)
file_response = urllib.request.urlopen(req)
binary_file = file_response.read()
print(binary_file)
When you make a call to print()
, the standard Python interpreter is going to wrap the binary contents in the binary string notation (b'string contents'). The extra characters are probably messing up GPG's read of the file. You can try removing the extra characters by hand if piping is really important to you or just do a quick write in the Python:
binary_file = file_response.read()
with open('file1', 'wb') as output:
output.write(binary_file)
(I don't understand your apparent aversion to this)
edit: You could also use the sys.stdout object:
binary_file = file_response.read()
import sys
sys.stdout.buffer.write(binary_file)