I'm very very new to python. I have a HTML code with POST method to upload a file, then I have python code to receive the file and save it to a FTP server. My python code so far is :
berkas = request.FILES['file']
filename = "test_send_file.jpg"
upload_folder = '/var/www/uploads/target_folder/'
handle_uploaded_file(join(upload_folder, filename), berkas)
Using those codes I got Permission Denied when uploading the file.
I know the missing part is where I have to set FTP server address and it's username & password, but I don't know how to that.
So, how to do it ?
Or, is there easier way to solve my "POST upload then save to FTP" problem ?
Thank you before.
Related
I am stuck with this problem that I have had for a while.
I have code here that is intended to upload a file to a web server.
class uploadFile_Functions():
def __init__(self, DESTINATION_URL, TARGET_FILE):
self.DESTINATION_URL = DESTINATION_URL
self.TARGET_FILE = TARGET_FILE
def uploadFile_WebServ(self):
dest_site = self.DESTINATION_URL
t_file = self.TARGET_FILE
with open(t_file,'rb') as f:
print('Uploading the file to the server')
upload_req = requests.post(dest_site, files={t_file:f})
print('Upload successful')
The dest_site will contain the link to the webpage folder that I have, and the t_file will contain the file to be uploaded.
What happens is that when I run this code, there were no errors thrown during runtime, and the shell printed the 'Upload successful' statement. But I checked the webpage folder in chrome, and the file is not even there.
I do not know if this needs some sort of authentication since this is a web server. I was able to get a file from the same webpage folder using the GET method, but no success so far with the POST method.
I appreciate any answer that I get from you guys. Thank you!
I want the user to be able to download a txt file that contains results of a sql query. I've seen answers speaking about using send_file or Response but all of those answers seem to require that I have the file stored?
Currently I have:
#RateRevisionEndorsements_blueprint.route('/_getEndorsements', methods = ['GET'])
def get_endorsements():
guid = request.args.get('guid')
client = Client()
# Save query results
result = client.getEndorsementFile(bookGuid = guid)
with open('tesult.txt', 'w') as r:
for i in result:
r.write(i)
return send_file("result.txt", as_attachment=True)
The button to generate this route works and I have no issue receiving the query results (currently stored as a list but I can make it whatever works best), but I receive the error FileNotFoundError: [Errno 2] No such file or directory: 'C"\\..\\app\\result.txt'
Which makes me think that I need it to have stored somewhere on the server to pull from.
Just send the data as a streaming response. Make sure to set the proper mime type so that the browser will initiate a download.
Exactly send_file sent files that are stored.
Sent it with the
Respone(file,mimetype=“txt/plain”)
first of all sorry for my English, it's not my mother tongue. I'm writing a XML parser in python 2.7. There are a links for images, that are downloading and saving into DB, it works fine with http, but I need to download them from ftp server.
Here is function that is calling from parser:
q=sa_web_images.objects.create(
web_image_url=iphoto,
web_item_id=wi_id,
non_repeat=non_repeat
)
q.get_remote_image()
Where 'iphoto' is a link, for example: ftp://example.com/image.jpg.
And here is function that save it into DB:
def get_remote_image(self):
if self.web_image_url and not self.web_image:
result = urllib.urlretrieve(self.web_image_url)
self.web_image.save(
os.path.basename(self.web_image_url),
File(open(result[0]))
)
self.save()
I know username and password for ftp access, but I don't know, how should I insert them into code, can someone help me please? Thanks.
username and password can be supplied to urllib by specifying them in the url, like for example
result = urllib.urlretrieve("ftp://user:password#example.com/image.jpg")
I'm trying to save files sent from C++ code(not by HTML)
But I do not know how to write the script to parse sent data and save it
C++ code works fine(from wireshark captured packet)
from Bottle tutorial: how do I edit it?
category = request.forms.get('category')
upload = request.files.get('upload')
thanks!
update:
I found that request.files works
files = request.files
for name, fobj in files.iteritems():
fobj.save(some_path)
If you are just sending the raw file in the HTTP request, access it with request.body.
http://bottlepy.org/docs/dev/api.html#bottle.BaseRequest.body
This is what I ended up doing:
from bottle import FileUpload
uploaded_file = FileUpload(request.body, None, filename='some_filename')
uploaded_file.save() # implement conflict resolution here, if needed
My school has a webdav file server that contains files that I frequently need to download. For this server, I have a username and password that I can use to connect to the server, and If I go to the URL in chrome I can view everything just fine. Now my question is, how can I access and login to this WebDAV server with python, and then download files from it. I have been unable to find anything with google and apologize if there was a very simple solution that I missed.
You can use python-webdav-library
from webdav import WebdavClient
url = 'https://somesite.net'
mydav = WebdavClient.CollectionStorer(url, validateResourceNames=False)
mydav.connection.addBasicAuthorization(<username>, <password>)
mydav.path = <path to file I want, ie '/a/b/c.txt'>
mydav.downloadFile(<local path ie. ~/Downloads/c.txt>)
Can't you use:
#codesnippet
import webbrowser
webbrowser.open("URL")
Replace "URL" with the web address of the file stored on the internet
server.