I want to download the file, it may be zip/7z. when I used the following code it is giving an error for the 7z file.
import requests, zipfile, StringIO
zip_file_url = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
try:
r = requests.get(zip_file_url, stream=True)
z = zipfile.ZipFile(StringIO.StringIO(r.content))
except requests.exceptions.ConnectionError:
print "Connection refused"
Just ensure the HTTP status code is 200 when requesting the file, and write out the file in binary mode:
import os
import requests
URL = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
filename = os.path.basename(URL)
response = requests.get(URL, stream=True)
if response.status_code == 200:
with open(filename, 'wb') as out:
out.write(response.content)
else:
print('Request failed: %d' % response.status_code)
The downloaded file will then appear in the directory where the script is being run if the request was successful, or an indication the file could not be downloaded.
Related
The method i used is to send an image to a chatbot and enter the server.
I want to send the image to the server without going through the chatbot.
I'm encoding the image file into base64 and sending it to the same port, but 400 or 500 errors keep coming out.
I was going to send it this way.
import os
import glob
import requests
import base64
folder_path = "./images"
image_files = glob.glob(os.path.join(folder_path, "*.jpg")) +
glob.glob(os.path.join(folder_path, "*.jpeg")) +
glob.glob(os.path.join(folder_path, "*.png"))
url="http://000000000/"
headers = {'Content-type':'application/json; charset=utf-8'}
for file_path in image_files:
with open(file_path, "rb") as file:
file_content = file.read()
encoded_file = base64.b64encode(file_content).decode()
data = {"file": (os.path.basename(file_path), encoded_file)}
r = requests.post(url, files=data, headers=headers)
print(f'{os.path.basename(file_path)} sent with status code {r.status_code}')
error message is :
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionAbortedError(10053
it is using ocr system which is in server
since i have to change the method to send imgae files. i wrote those codes but it's not working
I'm trying to rewrite the Unix command wget in python for a project I'm making for fun, and I need it to save a webpage as index.html and normal files with their normal file name. But everything I tried doesn't seem to work. Here is the code sample:
import os
import requests
user_input = "wget google.com"
parts = user_input.split()
if parts[0] == "wget":
# Check if the user just typed in wget
if user_input == "wget":
print(
"wget: missing URL\nUsage: wget [URL]..."
)
# If not, run wget
else:
try:
# Store the url the user put in in a variable
url = parts[1]
# Get the file name from the url
file_name = url.split("/")[-1]
# Destination
destination = os.path.join(os.getcwd(),file_name)
# Checking if the user typed in a url with http or https. If they didn't, it will add http:// to the url.
if not url.startswith("https://") and not url.startswith("http://"):
url = "http://" + url
# Send an HTTP GET request to the URL
response = requests.get(url)
# If the response status code is not 200, raise an exception
response.raise_for_status()
# Write the response content to the destination file
with open(destination, 'wb') as f:
f.write(response.content)
print("'{}' saved".format(file_name))
# Catching any exception
except Exception as e:
print("wget: An error occurred:", e)
print("Response status code:", response.status_code)
print("Response content:", response.content)
Putting this if statement after the response variable getting a value and placing the destination variable after this if statement worked pretty well. (sorry if i said it wrong.):
if "text/html" in response.headers["content-type"]:
filename = "index.html"
I am using Python 2.7. My Rest server side API works fine and I am able to upload a zip file using Postman. I am trying to upload a zip file using Rest client api. I tried requests package, but it is unable to send the files. I am getting an error : missing file argument.
This is my python server side code :
#ns.route('/upload_file', strict_slashes=False)
class Upload(Resource):
#api.expect(upload_parser)
def post(self):
args = upload_parser.parse_args()
file_nameup = args.file.filename
Here is the rest api client code :
import requests
from requests.auth import HTTPBasicAuth
import json
headers={'Username': 'abc#gmail.com', 'apikey':'123-e01b', 'Content-Type':'application/zip'}
f = open('C:/Users/ADMIN/Downloads/abc.zip', 'rb')
files = {"file": f}
resp = requests.post("https://.../analytics/upload_file", files=files, headers=headers )
print resp.text
print "status code " + str(resp.status_code)
if resp.status_code == 200:
print ("Success")
print resp.json()
else:
print ("Failure")
This is my error :
{"message":"Input payload validation failed","errors":{"file":"Missing required
parameter in an uploaded file"}
status code 400
Failure
In postman, I passed a zip file with in body with "file" as key and value as abc.zip file. It worked fine. I tried to use httplib library, but it fails as my post url does not contain port number. This the error with httplib :
python HttpClientEx.py
Traceback (most recent call last):
File "HttpClientEx.py", line 4, in
h = http.client.HTTPConnection(url)
File "c:\python27\Lib\httplib.py", line 736, in init
(self.host, self.port) = self._get_hostport(host, port)
File "c:\python27\Lib\httplib.py", line 777, in _get_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '// ....net/analytics/upload_file'
How to invoke rest url post and upload a file using urllib library. Please suggest any other ways to upload file in rest client. Thanks.
I found another duplicate post :
Python Requests - Post a zip file with multipart/form-data
The solution mentioned there did not work. I found that you need to provide the full path of the file, otherwise it will not work.
Use urllib3 module.
https://urllib3.readthedocs.io/en/latest/user-guide.html
Files & binary data
For uploading files using multipart/form-data encoding you can use the same approach as Form data and specify the file field as a tuple of (file_name, file_data):
with open('example.txt') as fp:
file_data = fp.read()
r = http.request(
'POST',
'http://httpbin.org/post',
fields={
'filefield': ('example.txt', file_data),
})
json.loads(r.data.decode('utf-8'))['files']
requests library worked with below changes in my code :
import requests
from requests.auth import HTTPBasicAuth
import json
from pathlib import Path
file_ids = ''
headers={'Username': 'abc#gmail.com', 'apikey':'123-456'}
# Upload file
f = open('C:/Users/ADMIN/Downloads/abc.zip', 'rb')
files = {"file": ("C:/Users/ADMIN/Downloads/abc.zip", f)}
resp = requests.post("https:// ../analytics/upload_file", files=files, headers=headers )
print resp.text
print "status code " + str(resp.status_code)
if resp.status_code == 201:
print ("Success")
data = json.loads(resp.text)
file_ids = data['file_ids']
print file_ids
else:
print ("Failure")
I am using the python requests library's Session feature to request dynamically generated images from a remote server and write them to a file. The remote server is often unreliable and will respond with an html document, or pieces of the image. What is the best way to verify that the content is indeed the right format (not html), and has completely loaded? (my formats are png and csv) An example of my code is as follows:
import requests
ses = requests.Session()
data = ses.get("http://url")
localDest = os.path.join("local/file/path")
with open(localDest,'wb') as f:
for chunk in data.iter_content()
f.write(chunk)
How would I modify this code to check that it is the right format, and is a complete file?
You have two options:
If the server gave correct information in the headers about the content, check that for an invalid content type or an invalid content length.
If the server is lying about the content type or sets a content length to the size of the incomplete image, validate the content afterwards.
The following does both:
import imghdr
import os
import os.path
import requests
import shutil
ses = requests.Session()
r = ses.get("http://url", stream=True)
localDest = os.path.join("local/file/path")
if r.status_code == 200:
ctype = r.headers.get('content-type', '')
if ctype.partition('/')[0].lower() != 'image':
raise ValueError('Not served an image')
clength = r.headers.get('content-length')
clength = clength and int(clength)
with open(localDest, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
if clength and os.path.getsize(localDest) != clength:
os.remove(localDest)
raise ValueError('Served incomplete response')
image_type = imghdr.test(localDest)
if image_type is None:
os.remove(localDest)
raise ValueError('Not served an image')
You can also install Pillow and validate the image further with that.
I would like to deploy artifacts using Python (with a zip file which is like a set of artifacts and the dir-structure should be preserved - according to the docs)
When I use the following docs nothing happens (no files created in the repo) but I get an OK response:
import httplib
import base64
import os
dir = "/home/user/"
file = "archive.zip"
localfilepath = dir + file
artifactory = "www.stg.com"
url = "/artifactory/some/repo/archive.zip"
f = open(localfilepath, 'r')
filedata = f.read()
f.close()
authheader = "Basic %s" % base64.encodestring('%s:%s' % ("my-username", "my-password"))
conn = httplib.HTTPConnection(artifactory)
conn.request('PUT', url, filedata, {"Authorization": authheader, "X-Explode-Archive": "true"})
resp = conn.getresponse()
content = resp.read()
How could I make it work?
Edit: Solved it!
I put up a local artifactory and played with it, curl and requests to figure out what was going wrong. The issue is that artifactory expects the file to be uploaded in a streaming fashion. Luckily, requests also handles that easily. Here is
code I was able to get working with an artifactory instance.
import requests
import os
dir = "/home/user"
filename = "file.zip"
localfilepath = os.path.abspath(os.path.join(dir, filename))
url = "http://localhost:8081/artifactory/simple/TestRepository/file.zip"
headers = {"X-Explode-Archive": "true"}
auth = ('admin', 'password')
with open(localfilepath, 'rb') as zip_file:
files = {'file': (filename, zip_file, 'application/zip')}
resp = requests.put(url, auth=auth, headers=headers, data=zip_file)
print(resp.status_code)