Implementing a remote shell server in python - python

I am writing a remote shell server in python.Here how it works.
1-The client connect to the server.
2-The server prompt the client to login.
2-After that the user can send a shell command to the sever.
3-The sever execute the command.
Here is a snippet of the server code that handle the coming requests
cmd=sock.recv(8192)
output = subprocess.Popen(cmd.split(),stdout=subprocess.PIPE).stdout.read()
sock.send(output)
My problem is that I want the server to handle multiple clients and I want every client to have a separate shell session.
edit:
I want the server to start a new instance of the shell whenever it receives a new connection from a client and keep that instance in memory until the connection closes.

You can implement it that for every connected client you server starts a thread, just for that client and handles his commands. Something like:
def client_handler(socket, address):
# receive client commands and do whatever is necessary
if __name__ == '__main__':
# Socket, Bind, Listen
while True:
# Wait for connection and accept
thread.start_new_thread(client_handler, (socket, address))

Related

How to make my client receive more than 1 response from server for every one request sent?

I am learning to write a Python client. It needs to be able to keep receiving responses of the current status of the request queue from the server when my client requests it. The server stops sending the status of the request queue when the client sends an off command.
Besides that, the client needs to have a prompt, <enter command>, printed out to the terminal so a new request can be sent to the server when a user enter a new command/request.
I was using select to make the client non-blocking. So the client can keep getting responses from the server while it is ready to read a user's command and send to the server. However, I wasn't able to control when the prompt, <enter command>, shows up. It ends up being displayed in the middle of the receiving of the multiple messages displaying the status of the request queue from the server.
Should my client use a blocking or non-blocking socket for the situation? Anything I missed? Thanks!

python socket server is hanging after sending data

I made a python socket server recently that listens on port 9777 the server is suppose to accept connections and once it does will allow you to send information to the client. The client will then print out whatever it received. However, I found that after I sent some data the server would hang until i reinitialized a new connection. Is there a reason for this and if so how can I prevent it from happening
The code of the server is :
import socket
import sys
host='0.0.0.0'
port=9777
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(10)
c,a=s.accept()
while True:
command=raw_input('[input>] ')
if 'data' in command:
c.send('continue')
data=c.recv(1024)
print data
else:
continue
the code will only send data if the word data is in the string. Here is the code for the client:
import socket
import sys
host='192.168.0.13'
port=9777
while True:
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
except:
continue
while True:
d=s.recv(9999)
print d
s.send('received')
My goal is to setup a connection between server and client. I want the server to be able to accept input from a user in a while loop and send the input to the client. The client needs to be able to receive information and when it does it will send a response to the server. Then the user can continue sending data to the server until they decide to terminate the program. However the server keeps hanging after sending data once to the client. Can anyone tell me how I can prevent that?
I try this code in my computer it's work fine , maybe you need to change host='192.168.0.13' to host='localhost'
and host='0.0.0.0' to host='localhost'
look at this picture
and if this problem stay maybe your ip address is the same of other device in the network for that try to run this command ipconfig /renew

One way web socket communication?

I was reading about the Python websocket-client library and realized that, to receive data, we have to start a connection:
from websocket import create_connection
ws = create_connection("ws://echo.websocket.org/")
print "Received " + ws.recv() + "..."
What if I just need a one-way connection? Say a Python script is running on my laptop, and it periodically sends messages to a local web server.
To receive messages, the web server would have to start a connection, but starting a connection requires a URL to connect to. My Python script is not a web server, so it lacks a URL. How could the web server receive messages from the script?
I tried to let the server listen for clients to connect with it via
ws = websocket.WebSocket()
while 1:
print 'received "' + ws.recv()
However, I get an error.
in _recv
bytes = self.io_sock.recv(bufsize)
error: [Errno 107] Transport endpoint is not connected
That error output leads me to believe that the server needs to connect in order to receive.
If you would want one way connection to the server, you could just listen on plain socket or use UDP or use HTTP requests ore any other TCP protocol.

Socket programming stuck waiting for a response from the server

For a class assignment I need to use the socket API to build a file transfer application. For this project there two connections with the client and server, one is called the control and is used to send error messages and the other is used to send data. My question is, on the client side how can I keep the control socket open and waiting for any possible error messages to be received from the server while not blocking the rest of the program from running?
Example code (removed some elements)
#Create the socket to bind to the server
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((serverName,portNum))
clientSocket.send(sendCommand) # Send to the server in the control connection contains either the list or get command
(If command is valid server makes a data connection and waits for client to connect)
clientData = socket(AF_INET,SOCK_STREAM)
clientData.connect((serverName,dataport)) #Client connects
recCommand = clientData.recv(2000) #Receive the data from server if command is successful
badCommand = clientSocket.recv(2000) #But if there is an error then I need to skip the clientData.recv and catch the error message in bad Command
when there is an error, the data-socket should be closed by the server, so recv ends automatically.

Python - Use an Unix socket which already exists

I run a Python program on an embedded system. This system runs a server that creates UNIX sockets.
def com(request):
my_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
my_socket.connect("/var/run/.json-cgi")
my_socket.send(request)
reply = my_socket.recv(4096)
return repr(reply)
/var/run/.json-cgi is the socket created by the server. I send a http request that I wrote before and I passed in the arg of the function.
When I print the reply, I just get ''.
I wonder if my program creates a new socket that has the same path than the server's one or if it uses the socket of the server.
I want to use the socket created by the server.
Did I forget something ?

Categories

Resources