Send file without MultipartPostHandler (Python) - python

I'm using MultipartPostHandler in file sending. My code is following:
params = {'file':open(file_name, 'rb')}
headers = {'cookie':session_id}
urllib2.install_opener(urllib2.build_opener(MultipartPostHandler.MultipartPostHandler))
response = urllib2.urlopen(urllib2.Request("www.example.com/upload", params, headers))
How could I do the same (send file to the server) without using MultipartPostHandler? It would be good to use only buid-in python modules and urllib2. Is it possible.

MultipartPostHandler needs to install it using easy_install, pip or
from source. I want like to write the python script that would not
require new instalations.
Just add it to your original script - it is just one file. Copy paste the code for the module.

Unfortunately, there is no direct method available to post a multiple part file using urllib2. But there are ways to accomplish that by writing a custom form object using mimetype and mimetools module. You could follow this recipe and adopt your form to do a mutipart upload using urllib2.
(In Python3, urllib.request data can take a pointer to file object and that does read the whole file into memory)

Related

How to receive URL File as parameter and save it to disk using FastAPI?

I would like to know if there is a way for FastAPI to receive a URL of a file as a parameter and save this file to disk? I know it is possible with the requests library using requests.get() method, but is it possible with FastAPI to receive the URL and save it directly?
I tried using file: UploadFile = File(...), but then it doesn't download the file when the URL is sent.
I don't believe so. I've come across this before and was unable to find a solution (and ended up using requests like you mentioned), but seeing this I wanted to check again more thoroughly.
Reviewing the uvicorn and fastapi repositories by searching the code itself, I see no functions/code that reference requests or urllib (they do use urllib.parse/quote, etc though) that would be 2 likely suspects to build requests. They do use httpx.AsyncClient, but only in tests. I would expect to see some use of these libraries in the main uvicorn/fastapi libraries if they had code to make external requests.
Seeing the above, I actually think I will change my code to use httpx.AsyncClient anyways since it is already a dependency.

Python requests - Put ignores file upload

I am trying to upload a file through python requests using the PUT method. But on the server side, the file is never received.
The code that I am using is:
files = {'test' : open(r"C:\Users\test.jar", 'rb')}
response = session.put(api_base + url.get('url').format(foo, bar),
headers=headers, data=data, files=files)
Does PUT ignore file uploads? Is it only valid for posts?
What am I doing wrong?
It seems that Python's request module does not work for file uploads if you use the PUT method as you did. The documentation only shows one way to use POST requests. I'm not sure if this is a bug or not working intentionally.
Others also have problems using PUT requests for file transfer, see e.g:
Cannot PUT file to Django REST API by python requests
Python requests PUT method creates a zero byte file
OP moved to PycURL, which seems to work and also seems to be a good alternative.

Python webbrowser not functioning with GIS server

I am trying to write a code that will download all the data from a server which holds the .rar files about imaginary cadastrial particles for student projects. What I got for now is the query for the server which only needs to input a specific number of particle and access it as url to download the .rar file.
url = 'http://www.pg.geof.unizg.hr/geoserver/wfs?request=getfeature&version=1.0.0&service=wfs&&propertyname=broj,naziv_ko,kc_geom&outputformat=SHAPE-ZIP&typename=gf:katastarska_cestica&filter=<Filter+xmlns="http://www.opengis.net/ogc"><And><PropertyIsEqualTo><PropertyName>broj</PropertyName><Literal>1900/1</Literal></PropertyIsEqualTo><PropertyIsEqualTo><PropertyName>naziv_ko</PropertyName><Literal>Suma Striborova Stara (9997)</Literal></PropertyIsEqualTo></And></Filter>'
This is the "url" I want to open with the web browser module for a particle "1900/1" but this way I get an error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
When I manually input this url it downloads the file without a problem.
What is the way I can make this python web application work?
I used a webbrowser.open_new(url) option which does not work.
You're using the wrong tool. webbrowser is for controlling a native web browser. If you just want to download a file, use the requests module (or urllib.request if you can't install Requests).
import requests
r = requests.get('http://www.pg.geof.unizg.hr/geoserver/wfs', params={
'request': 'getfeature',
...
'filter': '<Filter xmlns=...>'
})
print(r.content) # or write it to a file, or whatever
Note requests will handle encoding GET parameters for you -- you don't need to worry about escaping the request yourself.

Can I use CGI module to read and extract information from an uploaded FormData object file in python?

Can I use CGI module to read and extract information from an uploaded FormData object file in python?
The file is being uploaded on the server from the front end and I'd like to read the file and extract data from it.
So how do I go about it?
Try this minimal recipe using web.py. Remove debug output, write to a file, otherwise update to taste. E.g. you might want authentication at this endpoint.
It's a pip install web.py away.

Download a file from GoogleDrive exportlinks

Trying to download a file directly using Python and the Google Drive API exportlinks response.
Suppose I have an export link like this:
a) https://docs.google.com/feeds/download/documents/export/Export?id=xxxx&exportFormat=docx
To download this file, I simply paste it into the browser, and the file automatically downloads to my Downloads folder.
How do I do the same thing in Python?
EX: module.download_file_using_url(https://docs.google.com/feeds/download/documents/export/Export?id=xxxx&exportFormat=docx)
This is a repost of How do I download a file over HTTP using Python?
In Python 2, use urllib2 which comes with the standard library.
import urllib2 response = urllib2.urlopen('http://www.example.com/') html = response.read()
This is the most basic way to use the library, minus any error handling. You can also do more complex stuff such as changing headers. The documentation can be found here.

Categories