post file to a server

122 views
Skip to first unread message

Auden RovelleQuartz

unread,
Oct 14, 2019, 9:23:25 AM10/14/19
to web2py-users
Please let me know how to do this

SERVER_ONE: this is the application server
SERVER_TWO: this is the file storage server

The objective is to

>>> have the user select a file via a form (this is via a Web2py app running on SERVER_ONE)
>>> when user clicks the submit button, the file is posted to an API endpoint on SEVER_TWO
>>> the web2py app on SERVER_TWO then saves the file to disk


Here is my code for SERVER_ONE:

def file_upload():
form = FORM(
DIV(
INPUT(
_type = "file",
_name = "file",
requires = IS_LENGTH(MAX_IMAGE_UPLOAD_SIZE, MIN_IMAGE_UPLOAD_SIZE),
)
),
DIV(
INPUT(
_type = "submit",
_value = "UPLOAD IMAGE"
)
),
_method = "post",
_enctype="multipart/form-data",
)
if (form.process().accepted):
SERVER_TWO_APP_BASE_URL + "/default/get_file", 
files={'file': form.vars.file.file.read()}
)
return dict(
form = form,
)



Here is my code for in default.py controller of SERVER_TWO:

import requests 

def get_file():
try:
file = request.files['file']
file.save('', file.filename)
except Exception as e:
with open("_probe_20191013_001", "w") as f:
f.write(str(e))
with open("_probe_20191013_002", "w") as f:
f.write("\n\nthis is a test\n\n")
f.write(str(request))
return dict()

==============================================================

The file is not getting saved on SERVER_TWO

does anyone have any advice on how to accomplish that?

Val K

unread,
Oct 14, 2019, 1:01:40 PM10/14/19
to web2py-users
You don't need 'requests' on server_2, you can set 'allow-origin' to * + basic auth (I didn't test this variant, but I suppose it should work ), or you can perform GET some dummy request to server_2 to get origin and establish ssl (as option) and then post to login and then post file

Val K

unread,
Oct 14, 2019, 1:12:44 PM10/14/19
to web2py-users
Or you can just set 'allow-origin' to server_1

Auden RovelleQuartz

unread,
Oct 18, 2019, 12:22:27 AM10/18/19
to web2py-users
I found a solution

step one: install the Paramiko library on the application server (SERVER_ONE)

step two: import the paramiko library

step three: use the "putfo" method

here is an example

def file_upload():
import paramiko
form = FORM(
DIV(
INPUT(
_type = "file",
_name = "file",
)
),
DIV(
INPUT(
_type = "submit",
_value = "UPLOAD IMAGE"
)
),
_enctype="multipart/form-data",
)
if (form.process().accepted):
client = paramiko.SSHClient()
client.load_system_host_keys()
t = paramiko.Transport("www.server_two_url.com", 22)
t.connect(username = SERVER_TWO_LOGIN_STRING, password = SERVER_TWO_PASSWORD_STRING)          # for example:          t.connect(username = "root", password = "abc123")
sftp = paramiko.SFTPClient.from_transport(t)
sftp.putfo(form.vars.file.file, "/root/auden.jpg")
sftp.close()
t.close()
return dict(
form = form,
)

THAT WORKS FINE!  (on SERVER_TWO server using WinSCP I go to the /root folder on the VPS and there is the file)

Christian Varas

unread,
Oct 18, 2019, 12:57:08 AM10/18/19
to web...@googlegroups.com
Hi, I have an app that use CKeditor who send images to the server and the function who catch this file is more or less like this:

Server TWO
def ImgUpload():
    a = stuffs.Stuffs() #Custom library
    name = a.password(20)
    rand_name = a.password(20)
     
    
   #path where the files will be saved
    path_img = '/some/folder/here/'
    
    #save the data of the image in db (if you need it)
    db.images.insert(project_id=session.project_id, path=path_img,
                     rand_name=rand_name, name=str(name)+'.png')

    #save the file uploaded in disk
    open(path_img+str(name)+'.png', 'wb').write(request.vars['upload'].file.read())

in the server ONE goes the form that send the file to the server two.

this example does not contain any token to avoid that anyone can upload files.

Using sftp is good, but maybe can be a slower due the use of credentials


Cheers.
Chris.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/ee5f2127-18b8-4840-a298-a9d806100710%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages