python 用wget传递变量并检查结果

python pass variables with wget and check result

我对 python 很陌生,我尝试用 Wget 传递变量

代码:

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

我需要通过的是

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

然后读取接收到的数据

我怎样才能做到这一点?

相信我,我看了很多帖子,但我还是迷路了。

谢谢你:)

考虑到您必须使用 os.system() 的有点奇怪的约束,您可以像这样构造命令字符串:

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))

您可以调整命令结构以使用用户名:

user_name = 'james'
command = 'wget http://localhost/username={} -O {}'.format(user_name, dest_filename)