Python Forum

Full Version: Input function cutting off commands at spaces.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, Ive Posted on here about this script before and I had made some progress but now I have ran aground once again and hopefully you can help me. For whatever reason on line 19 On the client script I have an Input() function which cuts off each command entered at the space separating them. I don't get any trace back so sorry I don't have anything to post.
client:
#!/usr/bin/env python3

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if True:
	try:
		Host = input('Remote Host:')
		port = 4444
		s.connect((Host, port))
	except ConnectionRefusedError:
		print('connection refused.')
		s.close()
		exit()
	except socket.gaierror:
		print('connection refused.')
		s.close()
		exit()
while True:
    cmd = input('Host-->(R)Host:')
    cmd2 = (cmd).encode('utf-8')
    try:
    	s.sendall(cmd2)
    	data = s.recv(1060)
    	data2 = data.decode('utf-8')
    	print(data2)
    except BrokenPipeError:
    	print('The connection has been closed by the Host.')
server:
#!/usr/bin/env python3

import socket
import platform
import subprocess
s  = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Host = platform.node()
port = 4444
if  True:
	try:
		s.bind((Host, port))
		s.listen(1)
	except OSError:
		print('The script is already running.')
		s.close()
		exit()
conn, addr = s.accept()
while True:
    data = conn.recv(10)
    data2 = data.decode('utf-8')
    z = subprocess.run(data2, shell=True, stdout=subprocess.PIPE,	stderr=subprocess.PIPE)
    z2 = z.stdout+ z.stderr
    try:
    	conn.sendall(z2)
    except BrokenPipeError:
    	print('the connection was closed.')
I can't reproduce that. Commands with spaces are taken just fine on my machine. Why do you think the error is on line 19?

Why don't you put some debugging on the server side? Have it print out the commands that it receives. You could even have the client echo the commands that it reads before the commandstream from the server is printed.
(May-12-2021, 06:07 AM)bowlofred Wrote: [ -> ]I can't reproduce that. Commands with spaces are taken just fine on my machine. Why do you think the error is on line 19?

Why don't you put some debugging on the server side? Have it print out the commands that it receives. You could even have the client echo the commands that it reads before the commandstream from the server is printed.
I just figured it's line 19 because that's where the input() is and that seems to be where the problem, is for me it reads it as two separate commands.
(May-12-2021, 06:07 AM)bowlofred Wrote: [ -> ]I can't reproduce that. Commands with spaces are taken just fine on my machine. Why do you think the error is on line 19?

Why don't you put some debugging on the server side? Have it print out the commands that it receives. You could even have the client echo the commands that it reads before the commandstream from the server is printed.

I figured out that I accidentally deleted the "60" portion of the 1060 in the server side script Big Grin .