ip地址更改为整数

时间:2019-12-02 11:04:18

标签: python python-3.7 philips-hue

我正在制作一个脚本,该脚本将使用子进程模块来ping IP地址。但是当我尝试运行它时,它说:

Traceback (most recent call last):
  File "C:\Users\YUZi5\OneDrive\Desktop\huetest.py", line 99, in <module>
    subprocess.run("ping", int(hhip), "/dev/null", capture_output=True) #doesnt work rn
ValueError: invalid literal for int() with base 10: '192.168.1.109'

我已经查看了其他一些方法来执行此操作,但是它不起作用。有人可以帮忙吗。 完整的代码在这里:

from phue import Bridge
import subprocess


# https://github.com/studioimaginaire/phue
# in and for meaning: https://www.quora.com/In-Python-what-does-for-return-and-in-mean
# https://stackoverflow.com/questions/19845924/python-how-to-generate-list-of-variables-with-new-number-at-end
# the hub 192.168.1.107
while True:
    b = Bridge('192.168.1.107')

    b.connect() 
    b.get_api()

    b.get_light(1, 'on')
    b.get_light(1, 'name')

    lights = b.lights

    print ("NOTICE: Only 1 light is supported in this script more will be allowed in the future.")
    print ("This script is highly work in progress, contact Yusef (YUZi22) on Github if any errors occur")
    print()
    print ("Please make sure to input the local ip address of your hue hub, and press the button on it before you run this script")
    print()

    # Print names of all lights connected to hue hub.
    for l in lights:
        print ("Lights:")
        print(l.name)
        print()

    lselect = input("""Select Light (Top to bottom, has to be a number. Goes like this: light on top of list starts with 1 and as it goes down it will go 2,3,4,5 etc..)

Type here: """)


    if lselect == ('1'):
        print ("Light 1 selected")
        print()
        print()
        operation = input("""Select Operation:
1. Brightness 
2. Colour select
3. Turn off
4. Turn on
5. Brightness percentage
6. Hue hub connection test

(1 - 5)

Type here: """)

        select100per = ('255')
        select75per = ('191')
        select60per = ('153')
        select50per = ('127')
        select25per = ('63') #working on adding percentage brightnesses, i will work on it tmrw aswell

        if operation == ('1'):
            brightsel = input("Input a brightness value (0-255): ")
            brightop = l.brightness = int(brightsel)
            print ("Set brightness of", (l.name), "to", "Brightness:", (brightsel))

        if operation == ('2'):
            colhue = input("Set hue: ")
            colop = b.hue = int(colhue)
            colsat = input("Set saturation: ")
            colsaop = b.saturation = int(colsat)
            print ("Saturation set to", (colsat), "and hue set to", (colhue), "on", (l.name))

        if operation == ('3'):
            b.set_light([(l.name)], 'on', False)
            print ("Turned", (l.name), "off")

        if operation == ('4'):
            b.set_light([(l.name)], 'on', True)
            print ("Turned", (l.name), "on")

        if operation == ('5'):
            prcnt = input("What percentage of brightness do you want? 100%, 75%, 60%, 50% or 25% (More percentages will be added soon): ")
            if prcnt == ('100%'):
                cmdexec = l.brightness = int(select100per)
                print ("Set", (l.name), "to 100%")            
            if prcnt == ('75%'):
                cmdexec = l.brightness = int(select75per)
                print ("Set", (l.name), "to 75%")
            if prcnt == ('60%'):
                cmdexec = l.brightness = int(select60per)
                print ("Set", (l.name), "to 60%")
            if prcnt == ('50%'):
                cmdexec = l.brightness = int(select50per)
                print ("Set", (l.name), "to 50%")
            if prcnt == ('25%'):
                cmdexec = l.brightness = int(select25per)
                print ("Set", (l.name), "to 25%")

        if operation == ('6'):
            hhip = input("Please input the ip address of your philips hue hub: ")
            subprocess.run("ping", int(hhip), "/dev/null", capture_output=True) #doesnt work rn

        restart = input("Would you like to restart the script? (y/n): ")
        if restart == ('n'):
            ques = input("Are you sure? (y/n): ")
            if ques == ('y'):
                break
            if ques == ('n'):
                continue

查看if操作==('6'),以查看我要添加的特定部分。

1 个答案:

答案 0 :(得分:0)

您需要在subprocess.run方法中将参数作为列表传递。所以你的线

subprocess.run("ping", int(hhip), "/dev/null", capture_output=True)

应更改为

subprocess.run(["ping", hhip, "/dev/null"], capture_output=True)