When it comes to working in an organization, it’s quite common to write/develop code and deploy applications on your server. Secure Shell (SSH) is the communication protocol to gain access to a whole remote computer/server either on a local network or any server deployed in some other part of the world. PyScripter has a feature to create an SSH Client connection to an SSH server which then allows you to create your Python script remotely. Let’s get into the details of how to work with remote Python scripts and files using the PyScripter Python IDE.
Table of Contents
Prerequisites
- The PyScripter IDE should be installed. If not installed already, please find it at: PyScripter – Free Download – Embarcadero
- Familiarity in working with command-line tools like PowerShell and Terminal is helpful
Two types of SSH servers
- Local network server
- Remote server
Working with a Python script on the local network server
Let’s create a GNU/Linux SSH server. The setup I have is a Debian-based OS – PureOS running on my machine. I have a windows virtual machine running in which I installed PyScripter IDE. So the GNU/Linux-based PureOS will serve as an SSH server and the Windows virtual machine will serve as an SSH client in the local network. You can have a setup of two different machines connected to the same LAN or WiFi. Most of the recent operating systems have SSH pre-installed in them. On the GNU/Linux-based OS check whether an SSH server is already running with the following command:
1 |
systemctl status ssh |
If the server is already running, you’ll get a result similar to the above image where Active: active (running) with green color. If the server is inactive you’ll get a result similar to the below image;
To switch on the server enter the following below command and recheck the status using the above status command.
1 |
systemctl start ssh |
Now your server should be running. Please check the IP address of your server using the following command;
1 |
ip addr | grep inet |
The IP address is highlighted in the above image. Also, check the port number on which the SSH server is active using the following command;
1 |
sudo lsof -i -P -n | grep sshd | grep IPv4 |
The TCP port number(22) on which SSH is running is highlighted in the above image. We also need the username of your server. You can use the whoami
command to check your username.
How to connect to the SSH server from your local machine
Now let’s log in to the SSH server from the SSH client. I’ll use the Windows OS in my Virtual Machine as an SSH client. You can use a separate Windows machine that’s connected to the same LAN or Wi-Fi to create an SSH client. We can check whether OpenSSH client is already installed by opening the Settings > Apps > Apps & features > Optional features or otherwise add/install that feature as it’s shown in the below image;
Now, it’s time to login the SSH server as a client from the PyScripter installed Windows machine. We shall utilize the username, ip address and port number of the GNU/Linux Server which we noted down earlier. Please login to the server using the following command;
1 |
ssh USERNAME@IP_ADDRESS -p PORT_NUMBER |
My IP address will change here as I changed my Wi-Fi Hotspot compared to the above screenshots. Please observe the below image in my case where the IP address is changed from 192.168.0.108 to 192.168.85.1 as compared to the previous screenshot. The steps to log in to the SSH server are mentioned in the below figure;
Now, we’re logged into the SSH server computer where we can create, delete and mange files of the server from our SSH Client from the PowerShell. We can also use any other command line tools like git bash to implement the SSH Client on Windows. Here we’re logging in with the server computer’s password which can be compromised. We shall generate a public-private keypair to login into server instead of a password. To generate a SSH key based login pair, we can use the ssh-keygen
command as shown in the below figure;
We should now copy the public key in the id_rsa.pub file of the current Windows Client to authorized_keys file of the GNU/Linux Server. We shall use the following command to do it;
1 |
cat ~/.ssh/id_rsa.pub | ssh username@ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys" |
Observe the below image where “username” and “ip_address” are updated with my respective server details. We can then login to the Server without using any password.
How to edit a Python script on a remote server using PyScripter
We’re now set to work with remote files in PyScripter. Please open PyScripter and choose the File > Open Remote File option as shown below;
Follow the below mentioned sequence of steps to connect to an SSH server;
After adding the Server, we can now open any of the files in the server computer in the PyScripter. We should choose one of the servers from the list of added servers and the path of the file in the server. Use the below image as a guide;
Let’s check the content of check.py file in the Server computer to cross-check when it’s opened with PyScripter. As you click “OK” in the above image, the remote check.py file will get opened. Please check out the below figure where we open the check.py file with both the terminal and PyScripter.
After editing the Python file, save it and it’ll save to Server. Now, you can successfully edit, run and save the file to Server from the PyScripter IDE. Let’s now implement the functionality of accessing the file from a remotely hosted Server.
How to work with the Python script on the remote server
In real-time work structures in an organization, we host an application on a Remote Server. I chose Digital Ocean hosting to create a Remote Server. Digital Ocean generously offered me a referral link using which you can have $100 credit for 2 months. Use the following link:
https://m.do.co/c/1e4060599604
Use the above referral link and create an account. Create a Server – “Droplet” as they call it. Choose the basic options to create the server to test working with the remote server files in the PyScripter IDE.
We need to provide an SSH key when setting up the server for a password less login similar to what we’ve done with local server. Let’s create another SSH key-pair to use with the digital ocean server using the ssh-keygen
utility.
Finally, create the droplet and Digital Ocean’s droplet service will assign a public IP address to access the created server.
Let’s now try to access it (server) using PowerShell. Please open the PowerShell as an administrator to add the identity of the above created Server (droplet) to the Client computer. Check the below image for instructions;
Now we can successfully SSH and connect to the Server computer as you can observe in the below image;
How to create a Python script on a remote Linux server using PyScripter
Let’s now create an empty Python file in the Server and write some code in it by opening it using PyScripter IDE. We can use a UNIX command “touch <filename>
” to create a new file.
Now, open the PyScripter to open the above created Python file in the server. We can access the remote file from the remote server as we’ve accessed the file in the local server. We can list the SSH connection of remote server using its Public IP address similar to that we’ve done with local server. Open ( File > Open Remote File > > ) and then fill the server connection details in the Edit SSH Servers form/window.
Now the file at a given path in the remote server will open and we can work on it in the PyScripter IDE.
Save the edits using “Ctrl + S” shortcut and the file will be automatically saved in the remote server with changes. To demonstrate the saved changes, let’s check the content of the file in the remote server using the PowerShell connection. We shall use a UNIX based command “cat <filename>
” to view the content of the file in the terminal (PowerShell).
Summary of the things we learned about how to edit a Python script remotely
PyScripter IDE can become your goto IDE to work with remote files. You can do all the operations like open, create, edit, update and save the remote file right from your comfort of your favourite PyScripter IDE. There are various other features come along with PyScripter IDE to make it as your default IDE to work. We’re currently working Debug extensions adapter using which we can create debuggers for other programming languages. PyScripter can then be used to run other language codes like JavaScript, C language etc. Please download it at: PyScripter – Free Download – Embarcadero and give it a try. If you face any difficulty in following the above steps, please feel free to ask questions in the comments.