Make a python server receive files sent using curl - python

I want to make a python script that would run a server which I can then send a request to from another pc using curl to send a file. So I would send a file from a pc to the server using curl and then server would save that file.
I tried googling but couldn't seem to find anything like that, but I found that pycurl might be useful. If anyone could show me the direction towards my goal I would appreciate it.

Sending files is typically just a POST with data. You can make a webserver using something such as flask, and make an endpoint that receives post data, then saves it as a file.
On the server, you don't need curl at all, just an HTTP server that accepts requests.

You can use http.server in the standard library...
https://gist.github.com/mdonkers/63e115cc0c79b4f6b8b3a6b797e485c7

The server is mine, I will run the python script on my aws machine.
And the pc is windows 10, I would want to just use curl to send the
file which is pre installed on windows so that I don't have to install
anything or write any scripts. Purely send the file using curl from cmd
Sending the file via curl is likely to be problematic. When I Google curl upload file, I usually find people that have questions that were not answered or advice that does not work. Without writing a script you easy option is to use FTP. Curl would not be required on the Server. Either you write a simple script, or use FTP. The more difficult is on the PC side. Not all that difficult because you control both sides of the transfer.
Have you considered using a command line SFTP client? A secure FTP transfer is very easy using a CMD prompt. One single and simple CMD statement.
I did a web app for a laboratory. Every day PDFs with patient lab results are uploaded to a doctor portal. The docs then go to the portal to view the PDFs. I first wrote this web app 10 years ago. It has been very trouble free.
I was just beginning my answer, gathering some options when I got you message.
Let me know if FTP is a viable option. ASAP. I will get you the command line to FTP a file to the server.
pscp is a free SSH FTP file transfer utility. I have been using this for over 10 years. Very reliable. The date on my pscp.exe, a 316K byte file, is September 2013.
Putty also has another CMD utility psftp. I used it a few times. psftp has a scripting protocol.
Both Putty utilities are very popular and well documented.
This is a sample pcsp CMD. The RUN is the command from my Window app programming language as well.
RUN('CMD /c ECHO ON & pscp -v -p -pw $password $filename user#domain.com:server path & EXIT')
I went to Fillzilla Pro CLI (command line interface) Even though Filezilla CLI is poorly documented, Filezilla Pro supports transfers to and from These storage services:
Amazon S3
Backblaze B2
Box
Dropbox
Google Cloud Storage
Google Drive
Microsoft Azure File Storage Service
Microsoft Azure Blob Storage Service
Microsoft OneDrive
Microsoft OneDrive for Business
Microsoft SharePoint
OpenStack Swift
Rackspace Cloud
WebDAV
I would recommend the free psftp utility from Putty
The sftp.exe utility is a stand alone 734 KB file. No installation required just copy sftp.exe to any PC where you need it.
If FTP is an option.
Here is a link to the docs for psftp
These are the FTP commands it supports:
6.2.4 The quit command: end your session
6.2.5 The close command: close your connection
6.2.6 The help command: get quick online help
6.2.7 The cd and pwd commands: changing the remote working directory
6.2.8 The lcd and lpwd commands: changing the local working directory
6.2.9 The get command: fetch a file from the server
6.2.10 The put command: send a file to the server
6.2.11 The mget and mput commands: fetch or send multiple files
6.2.12 The reget and reput commands: resuming file transfers
6.2.13 The dir command: list remote files
6.2.14 The chmod command: change permissions on remote files
6.2.15 The del command: delete remote files
6.2.16 The mkdir command: create remote directories
6.2.17 The rmdir command: remove remote directories
6.2.18 The mv command: move and rename remote files
Content-Type: application/octet-stream
Content-Disposition: attachment
Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
Content-Disposition: form-data; name="fieldName"
<input type="file">

Related

How to always run specific python script on windows server manager 2016

i am new to the community and new in using servers and could use some help.
I am trying to setup an automatic JSON parser to another server using http post calls. The idea is as follows:
I manually put JSON files into a folder Input on the server
A python script that is always running on the server reads the files located within the folder
It reads the JSON files, posts all objects to another server, and moves the files to a "Processed" folder one file at a time.
I have been given a Windows Server, with Windows Server Manager 2016, and have managed to do the following:
installed python 3.8.2. on the windows server
able to run a python script using powershell
Installed NSSM to create a windows service
Now the windows server manager says i cannot resume or start the service that i tried to install via NSSM.
I am very new to servers, as well as python itself. Can somebody help me to get a python script running 24/7 on a windows server with windows server manager 2016?
Edit:
I managed to create a python script that can read files, upload them and move them to a processed folder, but i still have to run it by myself while i want it to always run on the server

run python through openssh

I have made a connection between my openssh client and server. Both sides are window machine. I fail to do the followings:
Enter the python command line interface on my SSH client side, which can be done by entering "python" directly through a command line.
I want to run a python script on a server side by calling it on my client side. This script calls a commercial library to download data from a commercial database.
I can do both perfectly through remote desktop on the server side, but when I use openssh to connect, all fail. Any way to solve, especially item (2)?
If Python is installed on the remote server as C:\Python27 and the script is C:\Foo\bar.py, then get PuTTY. It contains the program plink.exe which can execute remote commands.
The command for you should be
plink.exe user#remote C:\Python27\python C:\Foo\bar.py
as explained in http://the.earth.li/~sgtatham/putty/0.64/htmldoc/Chapter7.html#plink-usage

How can I automate remote deployment in python?

I want to automate the remote deployment which currently I am doing manually.
The process includes
Make the tar ball from certain folders
SFTP to the remote server
Rename the old folders
Untar the new tar file
Restart apache
The remote system is on the intranet and has no access to the outside internet
I want to know how can I transfer the file from my python script and then when the transfer is complete then log into ssh and do stuff. I am confused about how can I achieve that. On localhost and I can do all that but how can I do that on a remote host?
For simple&dirty work you can use fabric (This by no means say that you cannot use fabric to build serious product)
For heavy configuration routines, you'd better pick a CMS (e.g., ansible)

How to run a code in an Amazone's EC2 instance?

I understand nearly nothing to the functioning of EC2. I created an Amazon Web Service (AWS) account. Then I launched an EC2 instance.
And now I would like to execute a Python code in this instance, and I don't know how to proceed. Is it necessary to load the code somewhere in the instance? Or in Amazon's S3 and to link it to the instance?
Where is there a guide that explain the usages of instance that are possible? I feel like a man before a flying saucer's dashboard without user's guide.
Here's a very simple procedure to move your Python script from local to EC2 Instance and run it.
> 1. scp -i <filepath to Pem> <filepath to Py File> ec2-user#<Public DNS>.compute-1.amazonaws.com:<filepath in EC2 instance where you want
> your file to be>
> 2. Cd to to the directory in EC2 containing the file. Type Python <Filename.py> There it executed.
Here's a concrete examples for those who likes things shown step-by-step:
In your local directory, create a python script with the following code: print("Hello AWS")
Assuming you already have AWS already set up and you want to run this script in EC2, you need to SCP (Secure Copy Protocol) your file to a directory in EC2. So here's an example:
- My filepath to pem is ~/Desktop/random.pem.
- My filepath to py file is ~/Desktop/hello_aws.py
- My public DNS is ec22-34-12-888
- The ec2 directory where I want my script to be is in /home/ec2-user
- So the full command I run in my local terminal is:
scp -i ~/Desktop/random.pem ~/Desktop/hello_aws.py ec2-user#ec2-34-201-49-170.compute-1.amazonaws.com:/home/ec2-user
Now ssh to your ec2 instance, cd to /home/ec2-user (Or wherever you put your file) and Python hello_aws.py
You have a variety of options. You can browse through a large library of AMIs here.
You can import a vm, instructions are here.
This is a general article about AWS and python.
And in this article, the author takes you through a more advanced system with a combination of datastores in python using the highly recommend django framework.
Launch your instance through Amazon's Management Console -> Instance Actions -> Connect
(More details in the getting started guide)
Launch the Java based SSH CLient
Plugins-> SCFTP File Transfer
Upload your files
run your files in the background (with '&' at the end or use nohup)
Be sure to select an AMI with python included, you can check by typing 'python' in the shell.
If your app require any unorthodox packages you'll have to install them.
Running scripts on Linux ec2 instances
I had to run a script on Amazon ec2 and learned how to do it. Even though the question was asked years back, I thought I would share how easy it is today.
Setting up EC2 and ssh-ing to ec2 host
Sign up and launch an ec2 instance(Do not forget to save the certificate file that will be generated while launching ec2) with default settings.
Once the ec2 is up and running, provide required permissions to the certificate file chmod 400 /path/my-key-pair.pem (or .cer file)
Run the command: ssh -i /path/my-key-pair.pem(.cer) USER#Public DNS(USER data changes based on the operating system you have launched, refer to the below paragraph for more details && Public DNS can be obtained on ec2 instance page)
Use the ssh command to connect to the instance. You specify the private key (.pem) file and user_name#public_dns_name. For Amazon Linux, the user name is ec2-user. For RHEL, the user name is ec2-user or root. For Ubuntu, the user name is ubuntu or root. For Centos, the user name is centos. For Fedora, the user name is ec2-user. For SUSE, the user name is ec2-user or root. Otherwise, if ec2-user and root don't work, check with your AMI provider.
Clone the script to EC2
In order to run the scripts on ec2, I would prefer storing the code on Github as a repo or as a gist(if you need to keep code private) and clone into ec2.
Above mention is very easy and is not error-prone.
Running the python script
I have worked with RHEL Linux instance and python was already installed. So, I could run python script after ssh-ing to host directly. It depends on your operating system you choose. Refer to aws manuals if it's not installed already.
Reference: AWS Doc

Logging into a server and parsing files on that server with Python

I would like to be able to use a Python script that I wrote to search files to login to an Ubuntu server that's password protected (which I have credentials ), and search files on that server.. Is there a straight forward way to accomplish this?
To login and run remote terminal commands through python, you should use either paramiko or pexpect. Pexpect is not touched very much by noah these days... I'm starting to wonder whether he is abandoning it.
The other way is to sftp the files from the remote server to your local machine... paramiko is useful for that as well.

Categories