2

I'm writing some shell scripts to check if I can manipulate information on a web server.

I use a bash shell script as a wrapper script to kick off multiple python scripts in sequential order The wrapper script takes the web server's host name and user name and password. and after performing some tasks, hand's it to the python scripts.

I have no problem getting the information passed to the python scripts

host% ./bash_script.sh host user passwd

Inside the wrapper script I do the following:

/usr/dir/python_script.py $1 $2 $3

print "Parameters: "
print "Host: ", sys.argv[1]
print "User: ", sys.argv[2]
print "Passwd: ", sys.argv[3]

The values are printed correctly Now, I try to pass the values to open a connection to the web server: (I successfully opened a connection to the web server using the literal host name)

f_handler = urlopen("https://sys.argv[1]/path/menu.php")

Trying the variable substitution format throws the following error:

HTTP Response Arguments:  (gaierror(8, 'nodename nor servname provided, or not known'),)

I tried different variations,

'single_quotes' 
"double_quotes"
http://%s/path/file.php % value  

for the variable substitution but I always get the same error

I assume that the urlopen function does not convert the substituted variable correctly.

Does anybody now a fix for this? Do I need to convert the host name variable first?

Roland

2 Answers 2

1

You need to quote the string when doing variable substitution:

http://%s/path/file.php % value  # Does not work
"http://%s/path/file.php" % value # Works
'http://%s/path/file.php' % value # Also works
"""http://%s/path/file.php""" % value # Also works

Python does not do string interpolation the way that PHP does -- so there is no way to simply "embed" a variable in a string and have the variable be resolved to its value.

That is to say, in PHP, this works:

"http://$some_variable/path/to/file.php"

and this does not:

'http://$some_variable/path/to/file.php'

In Python, all strings behave like the single-quoted string in PHP.

Sign up to request clarification or add additional context in comments.

Comments

1

This:

f_handler = urlopen("https://sys.argv[1]/path/menu.php")

Should be:

f_handler = urlopen("https://%s/path/menu.php"%(sys.argv[1]))

6 Comments

Hi AJ,Thanks, your proposed solution worked. Why does Sean's solution not work? Also, what is the difference of embedding the input parameter within the "urlopen" function call versus outside? f_handler = urlopen("https://%s/path/menu.php"%(sys.argv[1])) f_handler = urlopen("https://%s/path/menu.php") %sys.argv[1] Is it because the argument is not passed to the functin?
I'm not certain whether Sean's solution works or not. As far as your second question, the format string must be followed by its arguments (the values enclosed in %()). If you put the argument list outside of the function, then they don't get passed to the format string and python can't expand it.
Thank you AJ, that's what I figured. By the way, incredible fast response to my questions. Thanks
@roland By the way, Sean's solution works just fine. It's a simple substitution. You don't need the parentheses, either.
@AJ, Thanks, it is supposed to be a simple variable substitution but I don't get Sean's solutions to work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.