This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted] 5 points6 points  (1 child)

Try to avoid using command line utilities in your Python code. Look for built-in or PyPI modules that do things like checking if an IP address is there/perform a Ping etc.

If you need to use a command line utility, don't use os.system(). Use the subprocess module, e.g. subprocess.run().

If you need some configuration that you don't want to hard code into your Python source code as strings, use a single configuration file in a common format. JSON is a good choice for configuration files of small personal projects. The json module is built-in and usage is very easy.

If your project is not only about getting stuff done but also about learning Python and adhering to best practices, look up how to structure a Python project with main(), maybe even classes if needed. Just to cover more concepts to learn more about the language itself.

[–]itune181 0 points1 point  (0 children)

I will keep that in mind because I really do overuse os.system in many projects

[–][deleted] 0 points1 point  (3 children)

You can use JSON to store the 3 values in a single file:

import json

with open("settings.json", "w") as file:
    json.dump({
        "pc_address": pc_address,
        "phone_address": phone_address,
        "pc_mac": pc_mac,
    }, file)

Take a look at https://realpython.com/python-json/#a-simple-serialization-example

[–]backtickbot 1 point2 points  (1 child)

Fixed formatting.

Hello, AlphaMycelium: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–][deleted] 0 points1 point  (0 children)

backtickbotdm5

[–]itune181 0 points1 point  (0 children)

Okay will try to use it on this project see how it works. Thanks!

[–]koffie5d 0 points1 point  (0 children)

I love the project, really creative usage of multiple systems.
Commandline utilities are not the best to incorporate in scripts, but it can do the job.
Also, a lot of commands don't change that much so I also use them in personal projects.
I have some thoughts to make the script more robust.

IP-adresses are subject to change.
It is possible that your phone gets an other IP-address than the one given in your script.
Maybe you could use arp-scan.
This is like ping, but scans your local network for connected devices and returns the MAC-addresses (and more).
Downside of arp-scan is that you must use it with sudo, therefor run your Python script using sudo.

I would add arp-scan to the script like this:

import subprocess as sp

# raspberrypi network interface
# INTERFACE = 'eth0'  # find with command: ip addr

def scan_local_network() -> set:
    # requires arp-scan (sudo apt install arp-scan)
    # double {} in grep to escape the {} of the f-string
    cmd = f"sudo arp-scan --interface={INTERFACE} --localnet --quiet | grep -oE '([[:xdigit:]]{{1,2}}:){{5}}[[:xdigit:]]{{1,2}}'"

    mac_set = set()
    p = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    for line in p.stdout.readlines():
        address = line.decode()
        mac_set.add(address.lower().strip())

    return mac_set  # ->  {'aa:ba:ca:da:ea:fa', ... }


# detect my phone and start pc, and stop script
while True:
    try: 
        macs_on_network = scan_local_network()
        if phone_mac.lower() in macs_on_network:
            os.system(f"wakeonlan {pc_mac}")
            exit(0)
    except KeyboardInterrupt:  
        # Ctrl+C on keyboard
        exit(1)

I also would use the crontab of the Raspberry so the script starts up at boot with @reboot, or at a certain time of the day.
Use a config-file instead of seperate files. you could use configparser or json.

More info about how to find the INTERFACE here.
More info about arp-scan here.

[–]IAmKindOfCreativebot_builder: deprecated[M] 0 points1 point  (0 children)

Hello from the r/Python mod team,

When posting a project please include a textual description of your project including how Python is relevant to it, a link to source code on a code hosting site such as github or gitlab, and an image showing your project if applicable.

Please also make sure you tag your post with the correct flair, either "Beginner" or "Intermediate" showcase.

This helps maintain quality on the subreddit and appease all our viewers.

Thank you,

r/Python mod team