Python Forum
Input function cutting off commands at spaces.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input function cutting off commands at spaces.
#1
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.')
Reply
#2
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.
Reply
#3
(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.
Reply
#4
(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 .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 943 Jun-02-2022, 01:53 AM
Last Post: kgall89
  pyttsx3 cutting off last word Extra 3 2,813 Feb-28-2022, 11:20 PM
Last Post: BashBedlam
  input function question barryjo 12 2,637 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,009 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,021 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,932 Oct-06-2021, 09:39 AM
Last Post: menator01
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,235 May-18-2021, 06:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020