I want to execute a Jupyter Notebook on a remote server.
If I use this:
with open('somefile.txt', 'a') as the_file:
the_file.write('Hello\n')
Is the file saved on the local or the remote file system?
Yes, the file will be saved into the remote file system. You can make use of applications like Filezilla if you are using Linux OS or you can use Winscp for windows to retrieve the files.
Related
I'm trying to run a local Python script from my laptop (which works fine) on remote server (VPS).
The script can't read local files from my laptop from VPS
Output:
My script on pycharm.
import sys
import time
import paramiko
# Connect to remote host
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myip', port=22, username='root', password='mypassword')
# Setup sftp connection and transmit this script
sftp = client.open_sftp()
sftp.put(r'/myscript.py', '/myscript.py')
sftp.close()
# till now everything's good. I check my VPS files i find my script uploaded there
# Run the transmitted script remotely without args and show its output.
# SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
stdout = client.exec_command('python3 /myscript.py')[1]
for line in stdout:
# Process each line in the remote output
print(line)
client.close()
sys.exit(0)
When I run script from VPS I got this issue
I can't run the script directly from VPS to check the issue because I use local files, check the screenshot:
When I remove local paths and run the script (both from pycharm and VPS), it works fine.
You cannot magically access local files from script run on a server.
I can't run the script directly from VPS to check the issue because i use local files, check img.
There's no difference between running the script in remote shell using your favourite SSH terminal client (I assume that's what you mean by "run the script directly from VPS") and running the script in remote shell using Paramiko. It still runs in the remote shell.
There's no easy way to make the client files accessible from the server. That would be a security nightmare.
Either your script has to upload the files to the server.
Or you need to run a (SFTP/FTP/whatever) server on your local machine to make your local file accessible to the world.
For an example how to run an SFTP server, see my guide:
Installing SFTP/SSH server on Windows using OpenSSH
How to delete specific file on Windows Remote Server using Python?
I'm able to connect to remote Windows Server using WMI, but not sure how to delete file remotely on windows server.
I try to run .bat file on remote machine.
From my machine i mapped the folder where my .bat (on the remote machine) is exist and from my machine i just try to run the file:
os.system(r'Y:\file.bat')
But it seems that nothing hapenning.
Any suggestions ?
BTW both machines are Windows
What are the different modules/ways to copy file from a windows computer to a linux server available in python
I tried using ftplib api to connect to the windows server but i m unable to do with the error - socket.error: [Errno 111] Connection refused
What are the other modules that i can connect to a windows computer to copy or list the files under a directory
If you have access to linux server, and the file generated on windows automatically, you can do the folowing:
Generate ssh-key on your windows maching
Add it to authorized_hosts of the linux machine
Install simple console scp tool on windows
Write simple cmd-script to copy file with help of scp, something like:
scp c:\path\to\file.txt user#linuxhost.local:/home/user/file.txt
Run this script automatically every time, then the file is generated on windows host.
How can I easily copy a local file to a remote server using python?
I don't want to map the drive to my local machine and the windows server requires a username and password.
The local machine is also a windows machine.
Examples I've seen are with linux and mapping the drive. Unfortunately that's not an option for me.
You can use the subprocess module or os.system to launch a command into a windows shell. Then you can use Powershell or cmd instructions.