在request.get(url,params=data)中使用str(sys.argv[1])



嗨,我正试图在request.get(url,params=data)数据列表中使用sys.argv。

我失败了这是我写的代码

#!/usr/bin/env python
import sys
import requests
from xml.etree import ElementTree
vlan = str(sys.argv[1])
url = "https://xxxx.xxx.com/api/"
data = {
    'key':'LUFRPT1VN2RyMmsxN0ljYWs1cdsaf3251VBkZHFLdHM9MEdHY3ZqWlpFQkI0UU1KeWpyZkV2OW9nM2pkSUE3NEdlRVVGSklWVTgvcz0=',
    'type':'op',
    'cmd':'<show><arp><entry name="ethernet1/5.608"/></arp></show>'
   }
output = requests.get(url,params=data)

我想用的是这里的

'cmd':'<show><arp><entry name="ethernet1/5.608"/></arp></show>'

我想从命令行获取usr参数,并使用它来代替608

所以当我调用脚本时/脚本123

我想用123作为这个

'cmd':'<show><arp><entry name="ethernet1/5.123"/></arp></show>'

我试着用

cmd':'<show><arp><entry name="ethernet1/5.$vlan"/></arp></show>'

不起作用

感谢

您要查找的功能称为"字符串格式化"。有两种变体,一种使用%运算符,另一种使用str.format方法。

%操作员:

'cmd':'<show><arp><entry name="ethernet1/5.%s"/></arp></show>'%(vlan)

str.format():

'cmd':'<show><arp><entry name="ethernet1/5.{}"/></arp></show>'.format(vlan)

最新更新