Python server with library socket - python

Im trying to build a python application that will be run on a specific port so that when i try to connect on that port the python application will be run.
Im guessing i have to do that with socket library but im not very sure about that.

On Linux you can do this with xinetd. You edit /etc/services to give a name to your port, then add a line to /etc/xinetd.conf to run your server when someone connects to that service. The TCP connection will be provide to the Python script as its standard input and output.

Related

How to connect Linux Virtual Machine from its host computer?

I'm creating a simple chat app using Python.
The server code is in the Linux VM (I am using Virtual Box), and the client code is in the Windows 10 computer where the Virtual Box is installed.
I'm trying to connect it with the Python socket.
It works when I ping both machines to each other.
My problem is what port should I put in the client code:
Client
import socket
import subprocess
cliente = socket.socket()
try:
cliente.connect(('192.168.1.33',9090))
cliente.send("1")
I tried every port available; however, nothing works. I think I am missing something to make this work.
First, You need to make sure that the port number passed to bind function in server code in Linux VM is the same port number used by connect function in your client.
Check this simple server-client example in python which is using port 12345 :
https://www.tutorialspoint.com/python/python_networking.htm
Second, (based on your comments) the IP address that should be used in client connect function is the IP address of the machine running the server code which is in your case the Linux VM. Try to run the shell command ifconfig in the Linux VM to get the IP address.
If this address didn't work you can change the network settings of the virtual machine to bridged instead of NAT and try again the ifconfig command and get the new IP address.
Doing SSH from the host machine to Linux Virtual machine will work great!!

Socket ssh connections remotely?

I wonder and i've been trying everything to get my program with python sockets to work remotely. When I say remotely is like, I run the server in my computer and my friend at his house can run the client and connect to my server. Is this possible without using Hamachi? Pls let me know, because I'm already dying by trying so many things and installing and uninstalling programs.
You have to activate port forwarding on your router so that everything that comes on the specific port is forwarded to your local IP (and the port should be open).

Socket programming python on real server

I'm following this http://www.raywenderlich.com/3932 for socket programming in iOS where the server coding is in PYTHON, however, I just want to know that according to this tutorial, the author used localhost and run the code from terminal such that python server.py to execute and listen for socket.
What I'm confusing is that, how can I make this command on real server, such that after putting the code of python in CGI-BIN, how can I run that from shell/terminal of a shared web hosting.
Here's my SSH Screenshot, where I tried to run that command to bind and listen for socket, but Here i'm failed as no JAVA LOGIN section is appearing in my case as the video tutorial shows.
My Question is, How can I run the command so that the server will listen for the port, as on my localhost.
The command is: python server.py
On a shared web hosting server you probably have a running web server for which you write scripts which generate some output for the web server to return to the client.
server.py however is no such script. It contains the code for an actual server. Running the command starts the server. Therefore you won't get this working by simply putting the file in a CGI-BIN folder. You do need to run the command.

Execute python code over SSH

I need to write a python script which connects to the host over SSH and then somehow connects to a service sitting on localhost and performs a little interactive session.
What first came to mind is to use Paramiko to do a local port forwarding and then use Pythons's sockets library to communicate with the service.
But working with Paramiko was quite a challenge and I haven't figured out how to fix some issues.
So I switched to pxssh and used just simple scenario:
conn.sendline('telnet {} {}'.format('localhost', port)
conn.expect('PASSWORD:')
conn.sendline(password)
...
But that telnet thing really bothers me.
And I think it's possible to establish SSH connection in such a manner that from Python's code prospective I just do data = open('somefile').read() which actually opens a somefile on a remote host and all traffic is being encrypted because of SSH.

Python socket is on different port to the one I ask for

I coded up the simple server and client on this page:
https://wiki.python.org/moin/TcpCommunication
In the code the port 5005 is specified, but when I run them, the server reports that other ports were used (e.g. 5807, 5810). Why is this?
(I'm running Anaconda python 2.7.8 through pycharm on Windows 7).
Different ports, because this Source port for client and it's taken randomly.
Target Server Socket 5005, must working fine.

Categories