python requests.get在添加variab时出错

2024-10-02 14:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的python脚本,当我尝试动态添加IP时,requests.get总是给我带来问题

如果我这样做,它会起作用:

r = requests.get("http://123.123.123.123")

如果我这样做,它就会失败:

r = requests.get("http://" + ipnum)

这是完整的代码

#!/usrbin/env python
import subprocess
import requests

ipnum = subprocess.check_output(["hostname", "-i"])
ipstr = str(ipnum)

r = requests.get ("http://" + ipstr)

if r.status_code == 200
    print("Web1:"+ str(r))
else:
    print("Web1:"+ str(r))

Tags: 代码importip脚本httpget动态requests