0

im so new to python im try to pass variables with Wget

code:

USERID = 201        
RES = os.system("wget http://localhost/ -O /usr/setting.txt")
if RES == error:
 print RES
else
 print 'good'

what i need to pass is

http://localhost/?userid=203 or username=james

and then read the received data

how can i achieve this?

trust me i looked at a lot of stuff posted but i'm still lost.

thank you :)

2
  • 1
    Why don't you use urllib2.urlopen() or the requests module to retrieve the URL? Do you need to store the response in a file? Commented Jul 5, 2016 at 1:08
  • @mhawke i've been asked to use os.system and yes i need to save the setting to file Commented Jul 5, 2016 at 1:14

1 Answer 1

1

Given the somewhat strange constraint that you must use os.system() you can construct the command string like this:

import os

user_id = 201    
dest_filename = '/tmp/setting.txt'
command = 'wget http://localhost/userid={} -O {}'.format(user_id, dest_filename)
res = os.system(command)
if res == 0:
    with open(dest_filename) as f:
        response = f.read()
        # process response
else:
    print('Command {!r} failed with exit code {}'.format(command, rv))

You can adapt the command construction to use a user name:

user_name = 'james'
command = 'wget http://localhost/username={} -O {}'.format(user_name, dest_filename)
Sign up to request clarification or add additional context in comments.

4 Comments

It's not my choice :( i have to use that.
how can i use multiple variables at once? eg. userid=10&cmd=list i tried command = 'wget localhost/username={}&cmd={} -O {}'.format(cmd, cmd, dest_filename) but it don't work
That will just add cmd twice. Do this: command = 'wget http://localhost/userid={}&cmd={}'.format(user_id, cmd, dest_filename).
But, now that you are adding multiple parameters you might be better off using urllib.urlencode() to construct a query string and append that to the base URL.

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.