Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading multiple raw_inputs
#1
Hello all,

My first post here. Newbie to python and I want to improve the below code mainly for learning purpose. Let me first post it and then explain what how I want to improve it. 

VAR1, VAR2, VAR3= raw_input("Enter ip, port and name seperated by colan").split(":")

template ="""frontend fe:{0}:bind:{1}
        mode tcp
        bind {0}:{1} name {2}"""
 
BLOCK = template.format(VAR1,VAR2,VAR3)
 
with open("file.txt", "a") as myfile:
    myfile.write(BLOCK)
As you can see I'm appending to the "file.txt" based on user inputs. Once the user specify the ip address and the port (i.e: 10.1.2.2:443:somename) it would construct the text in var "template"  sending the correct value to the corresponding "{}". I want to understand the best way to do the following.

-  I want to get the user to specify the raw_input as a command line argument (i.e ./script.py FE-<name>:<ip>:<port>  | BE-<name><ip>:<port>).

-  I want to add another part to the template called "backend bk:<somename>:<someip>:<someport>".  Script should pick up the correct user input to the right section (ie: "FE-<name>:<ip>:<port>" to the "frontend fe" section and "BE-<name><ip>:<port>" to the backend section.

-  once user is done inputting the parameters I want to restart a service i.e:service abc restart. Please note that I want to wait before user adds all the entries before restarting the service. 

Please help out gentleman. I really want to come up to speed.
Reply
#2
string.format() takes arguments, and makes those arguments available to the string via their index. So to add the backend, you could almost literally just copy/paste your frontend section... backend bk:{2}:{0}:{1}

Why are you using var1/var2/var3? Just give them real, meaningful names (maybe ip_addr, port, description?).

Quote:- I want to get the user to specify the raw_input as a command line argument (i.e ./script.py FE-<name>:<ip>:<port> | BE-<name><ip>:<port>).

I have no idea what you mean by that. Do you want your script to use command line arguments instead of raw_input()?

Quote:- once user is done inputting the parameters I want to restart a service i.e:service abc restart. Please note that I want to wait before user adds all the entries before restarting the service.

# do things
# save things to a file

# restart the service
import os
os.system("service abc restart") # or systemctl restart abc, depending on the flavor of linux
Reply
#3
(Apr-19-2017, 07:45 PM)nilamo Wrote:
# restart the service
import os
os.system("service abc restart") # or systemctl restart abc, depending on the flavor of linux

I believe that modern recommendation is to use subprocess module for executing shell commands
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
I thought that was only if you wanted to get the result/stdout of the command or if you didn't want to block while it ran, while os.system was fine for fire-and-forget type things?
Reply
#5
(Apr-19-2017, 08:16 PM)nilamo Wrote: while os.system was fine for fire-and-forget type things?
it's not okay to use os.system is deprecated.
There is an exact replacement,but should not be used because of shell=True.

After 3.5 they have named it better subprocess.run,which can be used for most stuff.
Reply
#6
(Apr-19-2017, 08:16 PM)nilamo Wrote: I thought that was only if you wanted to get the result/stdout of the command or if you didn't want to block while it ran, while os.system was fine for fire-and-forget type things?

subprocess module allows a lot of options. You may "fire and forget" - though I am not sure that's a good approach, and if you start a long process without saving reference to process object, how garbage collector will affect it.

You may capture output, redirect it to a pipe, to a file. Send input to process. Get execution results. Though I no longer see threats to discontinue os.system (I believe there were some), I tend to follow recommendations of documentation.

I am also not sure that os.system is "fire-and-forget" thing - if you don't redirect stdout, it will throw it it on your terminal
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading Multiple text Files in pyhton Fatim 1 1,912 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Reading csv with multiple "headers" Clives 3 2,496 Dec-31-2020, 09:25 AM
Last Post: Ana_junior
  Reading Multiple Lists Using SUM function dgrunwal 6 3,333 Jun-03-2020, 08:23 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

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