[SOLVED] Python how to assign user inputs to variables
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 how I want to improve it.
Code:
VAR1, VAR2, VAR3= raw_input("Enter ip, port and name seperated by colan").split(":")
##user input should be something like, 1.1.1.1:422:name1
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 variable "template" sending the correct value to the corresponding "{}".
I need to take this further and do the following. I'm really at a loss here guys.
- 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.
many thanks in advance
raw_input is for processing interactive user input.
If you want to pass and process command line args then what you probably need are optparse or argparse, depending on Python version.
As far as the other sections of the code, what to do with those args, it would be more helpful if you could show us what you have done and what parts you are having trouble with. Whlie people here are happy to help, we cannot write the code for you.
Last edited by astrogeek; 04-19-2017 at 01:42 PM.
Reason: hilight, typo
Thanks astro for the advice. Well I wrote the below code and it doesn't get me anywhere.
Code:
#!/usr/bin/python
import argparse
import re
import string
p = argparse.ArgumentParser()
p.add_argument("INPUT")
args = p.parse_args()
KKK= args.INPUT
bb=re.split(":|,", KKK)
def func_three(help):
for i in help:
#print help
return help
#func_three(bb[0:3])
YY = var1, var2, var3 = func_three(bb[0:3])
print YY
The way to run this script should be "script.py <IP>:<PORT:<HOSTNAME>". i.e: script.py 192.168.1.10:80:name1 172.25.16.2:100:name3
As you can see if one argument is passed I have no problems. But when there are more arguments I cant determine how to workout the regexes and get this done via a loop.
So to recap, this is how i want the output to look like to proceed further. Please help out.
Thanks all for trying to help out. I will try the suggestions out and get back to you. Its is very late in the night here in this part of the world/. Thanks once again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.