When someone installs my .whl file i should get the request on my webhook

I need to create a Python .whl file. When someone installs it using pip install myfile.whl, a webhook should send the system’s IP address and system name to a specified URL (https://webhook.site/).
but my programming is not working as expected

setup.py file

from setuptools import setup
from setuptools.command.install import install
import os
import requests

# Define a custom install command
class CustomInstallCommand(install):
    def run(self):
        # Run the original install command first
        print("Running CustomInstallCommand")
        install.run(self)
        # After installation is complete, trigger the webhook
        self.trigger_webhook()

    def trigger_webhook(self):
        url = "https://webhook.site/7d8000bd-212a-4163-b10a-30bb62e71ad1"
        try:
            response = requests.get(url)
            if response.status_code == 200:
                print("Webhook triggered successfully!")
            else:
                print("Failed to trigger webhook:", response.status_code)
        except Exception as e:
            print("An error occurred while triggering the webhook:", str(e))

# Setup the package
setup(
    name="my_package",
    version="45.3",
    packages=["my_package"],
    install_requires=["requests"],  # Add dependencies here
    cmdclass={
        'install': CustomInstallCommand,
    },
)

run_webhook.py

# my_package/run_webhook.py
import requests

def trigger_webhook():
    url = "https://webhook.site/7d8000bd-212a-4163-b10a-30bb62e71ad1"
    headers = {
        "Content-Type": "application/json",  # Use this or the appropriate content type
    }
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            print("Webhook triggered successfully!")
        else:
            print("Failed to trigger webhook:", response.status_code, response.text)  # Print response text for more details
    except Exception as e:
        print("An error occurred while triggering the webhook:", str(e))

init.py

# my_package/__init__.py
from .run_webhook import trigger_webhook

# Trigger the webhook when the package is imported
trigger_webhook()

I imagine this is an experiment, not for any production purpose, but lots of users would have a problem with software that phones home. It’s also likely illegal in the EU and UK under the GDPR, as IP addresses are considered personal data.

But, as a thought experiment, it might be possible.

Installing a wheel does not involve executing any code. Therefore to achieve this (not that it is a good idea) you need to publish the sdist and not a wheel.

Also pip builds and caches wheels so you would only be able to detect the first install on a system, not each install.

Also some systems install without Internet access, only a PyPI mirror, so the request and indeed the installation would fail.

9 Likes

Nothing gets executed when installing from wheel. There is no setup.py script in a wheel.

1 Like

This is a good advertisement for disabling sdist installation by default, IMO.

2 Likes

This same question, and possibly same poster, came up on the Python
Discord yesterday. I explained there that wheels executed no code. And
that I wouldn’t help them write spyware anyway.

5 Likes