Control Applications with python - python

lately I have been trying to find a way to control applications on my ubuntu using python. What i want to achieve is something like what we can do with applescript in mac.... Control chrome send queries to it etc.
can someone please point me in the right direction as to how one can control applications using python, esp on Ubuntu...or a code snippet maybe?

Read about D-Bus: http://en.wikipedia.org/wiki/D-Bus
Python dbus: http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
In order to control a process, it must be connected using dbus in the first place and exporting services. This is the same idea of AppleScript. On a mac, an application has to be exporting services that can be controlled over applescript.
PyQt/PySide has a DBus module as well: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtdbus.html

Have you taken a look at Project Sikuli?

Related

localhost server in compiled Python program

Background:
I am still new to Python so apologies if this is a naive question. I was originally looking to develop a tool for Windows with a GUI, however, reading around I get the impression that it may be simpler to have my compiled python code somehow 'wrapped' in simpleHTTPServer so users can interact via localhost (much like Jupyter notebook/labs).
Question:
Is there a standard/recommended way to have a compiled python program interface with the user via a localhost port in their web browser? I assume this is a fairly common approach and that a package may already even exist but I've not been able to find anything that gives guidance on how to tackle it.
Alternatively, is is feasible to somehow compile a Django development project to run on localhost?
Ideally, I want an executable file that users can run and their browser opens up at the localhost port required.
If you wish to quickly make a GUI based Python tool, you should consider using Django, a very straightforward framework for WebApps. It is very quick to learn and implement, and you shall very easily be able to integrate highly advanced functionalities too into your tool with ease.
When it comes to distribution of code, you can convert the entire Django App into a windows exe file for ease of distribution and use. There are many python packages that allow you to port python to windows executable files. I would recommend using PyInstaller. You may find this link useful: Recipe Executable From Django.
Alternatively, as someone suggested, you may use Docker, but that would be heavier in terms of space utilized, and will require the user to install the same.
Hope this helps!

How to control an outside executable application?

I have my own application on python 2.7. I want to control an outside .exe application.
I'm able to launch such an application, klm.exe as:
from win32com.client import *
ExtApp = Dispatch("Wscript.Shell")
ExtApp.Run("E:\XYZ\ABC\klm")
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
Yes, multiple ways, depending on the application.
Since you're already using COM (although I'm not sure why you're using it just to launch apps)… does the app have a COM automation (IDispatch) interface? If so, there will probably be documentation showing how to use it from VB# (or VBScript or C# or …), which you can easily adapt to Python and win32com. (For an example of such an application, see the Outlook automation docs.)
If there's no COM automation interface, there may still be a lower-level COM interface, which is almost as easy to use via win32com, but it usually won't provide any access to the GUI controls; instead, you'll be talking to the same lower-level functionality that the GUI uses. (For a good example, see Apple's iTunes COM Interface.)
If there's no COM support at all, the simplest thing to do is to automate it via Windows WM_* events. There are some examples of doing that in the pywin32 documentation, but there are also a lot of higher-level wrappers, like AutoPy and pywinauto/swapy, and so on that will make things a whole lot easier. There are dozens of these, free and commercial, and even more if you're willing to step outside of Python and use a different scripting system, and SO is not a good place to discuss the pros and cons of each.
Finally, you can always ignore the app's windows and just automate the system mouse… but this is almost always a silly thing to do.

Simple Python program to connect to a secure wifi network with user input credentials

My question I guess is: Is this possible without shelling out to command line and without 3rd party Python packages? I can't seem to find any native Python commands to manipulate or configure a wireless network connection. I know there are already built-in 'netsh wlan' commands in Windows 7, but would rather this all be in python. I am also confused by the logistics of this operation. With the netsh stuff, you still are required to have a wireless profile xml file specified in the command. My current image doesn't have any wireless profiles and I do not really understand the purpose of that if you are connecting to a brand new network. Why is this not automatically generated when you connect?
A little bit about the network
Network type: Infrastructure
Authentication: WPA2-Enterprise
Encryption: CCMP
The ultimate goal is to have a script that my users can just launch, put in their credentials, and never see the multiple Windows dialogues while doing so. I'm not asking for someone to write this for me. That's what I'm suppose to do. I just need to know if anyone has successfully done something like this in strictly Python or point me in the right direction. Thanks!
No. Python standard library doesn't ship with any functionality to control platform-specific functionality like wireless adapters. You have to invoke the tools shipped with the platform, find some 3rd party libraries that control this functionality, or write your own such libraries.

How do I interact with Windows programs

So I'm trying to create an automatic installer using Python. How would I go about interacting with native Windows applications that I launch? I am currently launching them using subprocess.call. Would I need to use Win32com? IronPython? Call a VB script from Python?
I've been trying to look into COM objects, but all I see are default Microsoft applications.
If the application supports COM, use COM using win32com. This tutorial helped me a lot. If it does not, you can use something like pywinauto or similar software. It is usually described as module for GUI testing. SendKeys comes handy for non-standard GUIs as a last resort.
Ranorex Spy is very helpful tool for identifying GUI elements.
Some of my colleagues also use compiled AutoHotkey scripts.

What are some successful methods for deploying a Django application on the desktop?

I have a Django application that I would like to deploy to the desktop. I have read a little on this and see that one way is to use freeze. I have used this with varying success in the past for Python applications, but am not convinced it is the best approach for a Django application.
My questions are: what are some successful methods you have used for deploying Django applications? Is there a de facto standard method? Have you hit any dead ends? I need a cross platform solution.
I did this a couple years ago for a Django app running as a local daemon. It was launched by Twisted and wrapped by py2app for Mac and py2exe for Windows. There was both a browser as well as an Air front-end hitting it. It worked pretty well for the most part but I didn't get to deploy it out in the wild because the larger project got postponed. It's been a while and I'm a bit rusty on the details, but here are a few tips:
IIRC, the most problematic thing was Python loading C extensions. I had an Intel assembler module written with C "asm" commands that I needed to load to get low-level system data. That took a while to get working across both platforms. If you can, try to avoid C extensions.
You'll definitely need an installer. Most likely the app will end up running in the background, so you'll need to mark it as a Windows service, Unix daemon, or Mac launchd application.
In your installer you'll want to provide a way to locate a free local TCP port. You may have to write a little stub routine that the installer runs or use the installer's built-in scripting facility to find a port that hasn't been taken and save it to a config file. You then load the config file inside your settings.py and whatever front-end you're going to deploy. That's the shared port. Or you could just pick a random number and hope no other service on the desktop steps on your toes :-)
If your front-end and back-end are separate apps then you'll need to design an API for them to talk to each other. Make sure you provide a flag to return the data in both raw and human-readable form. It really helps in debugging.
If you want Django to be able to send notifications to the user, you'll want to integrate with something like Growl or get Python for Windows extensions so you can bring up toaster pop-up notifications.
You'll probably want to stick with SQLite for database in which case you'll want to make sure you use semaphores to tackle multiple requests vying for the database (or any other shared resource). If your app is accessed via a browser users can have multiple windows open and hit the app at the same time. If using a custom front-end (native, Air, etc...) then you can control how many instances are running at a given time so it won't be as much of an issue.
You'll also want some sort of access to local system logging facilities since the app will be running in the background and make sure you trap all your exceptions and route it into the syslog. A big hassle was debugging Windows service startup issues. It would have been impossible without system logging.
Be careful about hardcoded paths if you want to stay cross-platform. You may have to rely on the installer to write a config file entry with the actual installation path which you'll have to load up at startup.
Test actual deployment especially across a variety of firewalls. Some of the desktop firewalls get pretty aggressive about blocking access to network services that accept incoming requests.
That's all I can think of. Hope it helps.
If you want a good solution, you should give up on making it cross platform. Your code should all be portable, but your deployment - almost by definition - needs to be platform-specific.
I would recommend using py2exe on Windows, py2app on MacOS X, and building deb packages for Ubuntu with a .desktop file in the right place in the package for an entry to show up in the user's menu. Unfortunately for the last option there's no convenient 'py2deb' or 'py2xdg', but it's pretty easy to make the relevant text file by hand.
And of course, I'd recommend bundling in Twisted as your web server for making the application easily self-contained :).

Categories