How to expose part of Python program as COM server - python

I have python application (bundled using py2exe) that runs as background system-tray application which monitor specific folder (like Dropbox) for any file add/create and backup that file into the cloud.
I want one feature added to that application i.e. expose API as COM server so another application can use it by passing the file path and get that file backed-up into cloud.
I have gone through this link on how to create COM server and I feel I'm close to get this things happen with following issue:
Every time I run my application it register the COM and hence ask Administrator's permission
What is the way to register COM without asking Administrator permission. My application distributed as installer made by Inno setup.
I need my application open automatically (if it is closed) when other application access COM object (registered by my application)

Related

Installing and running pywinauto in a remote desktop

I want to automate a very old software (drafix) that can only be run in Windows XP, on a remote desktop.
Would it be possible to write a pywinauto script that would directly interact with the software controls, without the need to send mouse/keyboard clicks on the GUI, and then minimize the Remote Desktop screen or even lock the PC?
I need to open the program, loop through a list of file names, open each one of them and save as a different format.
I did it with some basic GUI automation - clicking on buttons if necessary, sending keyboard shortcuts and entering the file names form a list. But it isn't very reliable, and I would like to minimize the Remote Desktop window and use my PC instead of having it in the foreground.
From what I have read here, it should be possible with some workarounds, and I would need to install an older version of Python (any idea which one?) on the remote desktop to write and run the script there.
What you can do is just copying your script to remote machine and run it there. Regarding minimized or closed RDP there are few workarounds and tricks described in the Remote Execution Guide which summarizes few StackOverflow answers.
There is other way that you can control gui application on remote Desktop with pywinauto. You can use rpyc or Remote Python Call. that is a python library for rpc(remote procedure call)
https://rpyc.readthedocs.io/en/latest/
your remote computer is as server and your host is as client in rpyc.
first you you need to run rpyc_classic.py in remote computer after connect to it and use pywinauto library. for example i use rpyc for start microsip on remote computer with pywinauto.
ip of remote computer is 192.168.222.222.
import rpyc
ip = "192.168.222.222"
conn = rpyc.classic.connect(ip)
conn.execute("from pywinauto import Application")
conn.execute(r"Application().start(r'C:\Program Files (x86)\MicroSIP\microsip.exe')")

Can an application started by a windows service connect to internet

I have a script in python whose purpose is to make sure that a particular application that continuously download data from internet stays up all the time. If the application crashes, the script brings it back up.
Since the system is in Windows, I modified the script into a service, so that even if windows restarts itself, the service will ensure that the application is up even if the user is not logged in.
The service was installed using an account that has admin permissions. The system seems to work fine except that the application that is successfully brought up is not connecting to internet (The application writes log file, so we can be sure that it started fine). Faces the same issue if the user is changed to Network Service or Local System
So I was wondering if there are any permissions required to help it connect to internet (or is this not possible at all).
You probably need to specify an account that can run the application normally on the service's Log On tab (of the Services.msc control panel application):
Additional details in the Windows Services FAQ.

Run executable in Desktop mode with Django / Python on IIS / Windows

The idea:
There is a Node.JS server which sends a request to the IIS server which is running Django / Python. It will send two files to the server which need to be converted with a program which needs to be run in the foreground mode.
So I already looked around in pretty much everything related to IIS and the running of executables here on SO, but still haven't got it working.
I got the following code to run the application from Django:
subprocess.call("C:\example.exe")
There will be probably be some serious security issues (although the server is only reachable from the local network) with the following setup, but here it is:
I'm running a Django application on IIS.
I've set the Application Pool Identity to my local user
I've given "Full Control" permission to "Everyone"
When the subprocess call gets executed it will add the program to my Background Processes with the USER being set to my local user.
Questions:
How do I make the program start in Desktop Mode?
Should I perhaps add another step (start another service) which will then start the program?
Edit:
Could I perhaps make a file watcher which watches if files get stored on the Windows Server and then triggers an executable based on that?

Python distributed application architecture on constrained corporate environment

This is my scenario: I developed a Python desktop application which I use to probe the status of services/DBs on the very same machine it is running on.
My need is to monitor, using my application, two "brother" Window Server 2003 hosts (Python version is 2.5 for both). One of the hosts lies in my own LAN, the other one lies in another LAN which is reachable via VPN.
The application is composed by:
A Graphical User Interface (gui.py), which provides widgets to collect user inputs and launches the...
...business-logic script (console.py), which in turn invokes slave Python scripts that check the system's services and DB usage/accounts status/ecc. The textual output of those checks is then returned back to the GUI.
I used to execute the application directly on each the two machines, but it would be great to turn it into a client/server application, so that:
users will just be supposed to run the gui.py locally
the gui.py will be supposed to communicate parameters to some server remakes of console.py which will be running on both of the Windows hosts
the servers will then execute system checks and report back the results to the client GUIs which will display them.
I thought about two possible solutions:
Create a Windows service on each of the Windows hosts, basically executing console.py's code and waiting for incoming requests from the clients
Open SSH connections from any LAN host to the eliged Windows host and directly run console.py on it.
I am working on a corporate environment, which has some network and host constraints: many network protocols (like SSH) are filtered by our corporate firewall. Furthermore, I don't have Administration privileges onto the Windows hosts, so I can't install system services on them...this is frustrating!
I just wanted to ask if there is any other way to make gui.py and console.py communicate over the network and which I did not take into account. Does anyone have any suggestion? Please note that - if possible - I'm not going to ask ICT department to give me Administration privileges on the Windows hosts!
Thanks in advance!
Answer to myself: I found one possible solution..
I'm lucky because the console.py script is actually invoking many slave python scripts, each of them performing one single system check via standard third-party command-line tools which can be fired to check features on remote hosts.
Then, what I did was to modify the gui.py and console.py so that users can parametrically specify on which Windows host the checks must be carried out.
In this way, I can obtain a ditributed application...but I've been lucky, what if one or more of the third-party CL tools did not support remote host features checking?

How can I create an local webserver for my python scripts?

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.
I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.
Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.
Or am I missing something obvious?
Don't waste a lot of time creating Windows service.
Don't waste a lot of time on Windows Apache.
Just make a Python service that responds to HTTP requests.
Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.
Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.
Also http://fragments.turtlemeat.com/pythonwebserver.php
"I'm trying to avoid making the user run python stuff from the command prompt."
I don't see how clicking a web page is any different from clicking desktop icons.
Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.
Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.
You can put the server in the user's start-this menu.
You can make your Python program a windows "service".
Best way is to make your own local server by using command prompt.
Make a new folder say Project
Make a new folder inside project & name it as "cgi-bin"(without quotes)
Paste your .py file inside the cgi-bin folder
Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
The wasiest step would be navigate to folder where your files are located and running http.server module
cd /yourapp
python3 -m http.server
the you should see something like this in console
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.
BTW: I had a similar question on stackoverflow: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

Categories

Resources