asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.13k stars 1.23k forks source link

pyautogui does not work as windows service #869

Open Humburto opened 1 month ago

Humburto commented 1 month ago

I wrote a simple app with pyautogui and flask as follow. When I request http://{ip}/, the "windows" key should be pressed(so the windows start menu should appear).

import pyautogui
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    pyautogui.press('win')
    return "hello world"

if __name__ == '__main__':
    app.run(host="0.0.0.0", port="80")

When I run this app in a terminal, it works as expected. However, when I warp it to a windows service with nssm(the non-sucking service manager), it does not work any more.

When I request the url, pyautogui raise some errors about failsafe:

[2024-08-02 22:05:12,577] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "D:\code\media-remote\.venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\code\media-remote\.venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\code\media-remote\.venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\code\media-remote\.venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\code\media-remote\test.py", line 8, in index
    pyautogui.press('win')
  File "D:\code\media-remote\.venv\Lib\site-packages\pyautogui\__init__.py", line 593, in wrapper
    failSafeCheck()
  File "D:\code\media-remote\.venv\Lib\site-packages\pyautogui\__init__.py", line 1734, in failSafeCheck
    raise FailSafeException(
pyautogui.FailSafeException: PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED.

I tried set pyautogui.FAILSAFE= False to disable failsafe then it did not raise above errors, but the windows start menu also did not appear( which means the windows key was not pressed).

Any help will be appreciated, thanks.

henygebremedhin commented 1 month ago

import os import pyautogui from flask import Flask, request, abort

app = Flask(name)

Environment variable to control pyautogui actions

ENABLE_PYAUTOGUI = os.getenv("ENABLE_PYAUTOGUI", "False") == "True"

def check_authentication():

Replace with your authentication logic

auth_token = request.headers.get("Authorization")
if auth_token != "your-secret-token":
    abort(403)

@app.route("/") def index(): check_authentication() if ENABLE_PYAUTOGUI: pyautogui.press("win") return "Hello World"

if name == 'main': app.run(debug=True, host="127.0.0.1", port=5000)

From: Humburto @.> Sent: Friday, August 2, 2024 10:23 AM To: asweigart/pyautogui @.> Cc: Subscribed @.***> Subject: [asweigart/pyautogui] pyautogui does not work as windows service (Issue #869)

[External Email]

I wrote a simple app with pyautogui and flask as follow. When I request http://{ip}/http://%7bip%7d/, the "windows" key should be pressed(so the windows start menu should appear).

import pyautogui

from flask import Flask

app = Flask(name)

@app.route("/")

def index():

pyautogui.press('win')

return "hello world"

if name == 'main':

app.run(host="0.0.0.0", port="80")

When I run this app in a terminal, it works as expected. However, when I warp it to a windows service with nssm(the non-sucking service manager), it does not work any more.

When I request the url, pyautogui raise some errors about failsafe:

[2024-08-02 22:05:12,577] ERROR in app: Exception on / [GET]

Traceback (most recent call last):

File "D:\code\media-remote.venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app

response = self.full_dispatch_request()

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\code\media-remote.venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request

rv = self.handle_user_exception(e)

     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\code\media-remote.venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request

rv = self.dispatch_request()

     ^^^^^^^^^^^^^^^^^^^^^^^

File "D:\code\media-remote.venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]

       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\code\media-remote\test.py", line 8, in index

pyautogui.press('win')

File "D:\code\media-remote.venv\Lib\site-packages\pyautogui__init__.py", line 593, in wrapper

failSafeCheck()

File "D:\code\media-remote.venv\Lib\site-packages\pyautogui__init__.py", line 1734, in failSafeCheck

raise FailSafeException(

pyautogui.FailSafeException: PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED.

I tried set pyautogui.FAILSAFE= False to disable failsafe then it did not raise above errors, but the windows start menu also did not appear( which means the windows key was not pressed).

Any help will be appreciated, thanks.

- Reply to this email directly, view it on GitHubhttps://github.com/asweigart/pyautogui/issues/869, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AHELSLJ7FEDCIPHKU5GSLCTZPOI3VAVCNFSM6AAAAABL4UUM5SVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ2DKMBWHA2TQNA. You are receiving this because you are subscribed to this thread.Message ID: @.**@.>> This email contains links to content or websites. Always be cautious when opening external links or attachments.

Please visit https://carleton.ca/its/help-centre/report-phishing/ for information on reporting phishing messages.

When in doubt, the ITS Service Desk can provide assistance. https://carleton.ca/its/chat -----End of Disclaimer-----