Control an existing application with Python - python

I have been programming with Python for some time and noticed that it's possible to interact, for example, with MS Excel files through the library XLWT.
Now I would like to know if it's possible to use Python to control other applications, such as the Calculator.exe which is on the standard Windows path C:\Windows\system32.
Is there any way to write a script with Python, let's say, to make the calculator opening and executing 9+3=? I usually like to write some code myself first and ask for help later, but here I've no clue even if it's possible and my researches on Internet have yield only this script to launch the program:
import subprocess
subprocess.call("C:\Windows\system32\calc.exe")
Any help, suggestion or even just "no, it's not possible" would be highly appreciated.

It will always depend on the cooperativity of the other program. If it allows being tweaked, it will offer an API for this (and hopefully a documentation telling you how to use it).
This is not really a Python question because it rather depends on how the API is written. If this API comes as a C library, you will have to write at least a bit of C code to access it via Python. If it is a way of calling the program (special options, etc.) then Python will have as much or less trouble providing these as any other programming language.

Related

Objective-C functions vs. Python modules?

Just another newbie question I'm afraid. I am doing a course on python on udacity and pretty much straight away have been shown modules, such as time, and webbrowser, which make it very easy to do such tasks as time delay, opening webbrower, etc.
Is there anything so straightforward and easy to use as this in Objective-C, or what would be the equivalent to the python modules? Is it just the case that you need to write a lot more code in Objective-C alongside their provided library functions to achieve the same thing as a Python module would provide you with?
It just seems like Python modules have more im-built code with less for developer to write in code?

Obfuscating tool for Python3 code

Is there any existing python code obfuscating tool for Python3?
Please do not try to teach me that Python isn't the right choice if I want to hide/obfuscate my code. Or that correct licenses should protect the code instead of obfuscation...
Update: This question does not duplicate issue How do I protect Python code?: I simply ask if there is a tool to obfuscate Python. Nothing more and nothing less. (If there is none I wonder why I get so much feedback...)
No matter what you do, at some point the Python interpreter is going to be reading in unobfuscated Python byte-code. From that it is dead easy to get back to your source code (minus comments and non-obvious layout). This is why everybody says it's pretty much impossible to obfuscate Python. The fact that it's pretty much impossible to obfuscate Python implies that there are no good tools for doing so. I'm afraid it's just wishful thinking to say "I know this can't be done very effectively, but are there any tools for doing it?"
Probably the best you can do will be to encrypt your code with standard encryption tools, and write a little wrapper program in some other language that just decrypts your Python and runs your program, then deletes the unencrypted code when it's done. If you want to put way too much effort in you could probably do something with the C API and embedding the Python interpreter in a C program to feed your unencrypted Python to the interpreter only in memory, rather than files on disk.
Note that these schemes will still be relatively easy to get around, and don't work at all if what you want is to provide importable Python modules (rather than whole programs). Which is why I wouldn't expect to find anyone's already written a tool for you to do it.
I wouldn't go the obfuscating approach if I were you and rather investigate alternative ways to ship executable binary files instead of (byte)-code.
Tools that are known to me (there are probably a few others):
http://www.ohloh.net/p/py2c (converts Python into C code which you can then compile)
http://www.pyinstaller.org/
http://cx-freeze.sourceforge.net/
http://www.py2exe.org/ (Windows only)
http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html (Mac only)
I don't know how hack-proof any of those tools are, but I think it's worth taking a look.
Edit: Damnit, missed the Python 3 part. It's a little hard to help because you don't write anything about the product itself (OS, GUI, etc). If it can be also Python 2 code but you have written all your code in Python 3 already, I suggest 3to2.
I'd recommend using pyarmor.
It converts code to binary form. Only drawback would be, you need to obfuscate code for every OS separately.
There is no way to obfuscate Python code in any useful manner, and no reason why you would want to. You seem to want to obfuscate the code to protect it. That is completely pointless, as you can instead ship only the .pyc files, meaning you don't ship the source code at all. Not that shipping only .pyc files will help you, as there are uncompilers for .pyc-files.
If your program is reasonably simple and well-coded, creating executables with cx-freeze, py2exe et al, means that the .pyc files end up inside the executable file, and hence are marginally harder to find, and it's also less obvious that you use Python, so that might be help. But more importantly, it might make installation simpler for your users. They like that.
If you really want to obfuscate your code in a useful way, convert all of it to use Cython, which will create C-files you can compile. This will also speed up the program. Cython is however not fully Python compatible, so you will probably have to make changes.
And I know you don't want to hear this, but I'll say it for the benefit of others:
All of this is of course stupid and misguided. Open source is good for you. Really. You shouldn't protect your code, you should get as many eyes and hands on it as possible.
Trust me on this: Your main worry should be about getting more users, not less pirates. And you get more users by making your software better, not worse. And open source will help in that.
Pyminifier is a Python code minifier, obfuscator, and compressor.
This tool works on Python 3
Update: This project has been discontinued.
The best way to hide your code is to not release it.
Advertise a service - you receive their data then return the processed data. Transmissions can be via the web, email, DHL, pigeon, telephone, graviton pulse, ...

Python: Compiling Script Safely?

Greetings!
So contrary to my last question I think I was pursuing the wrong method. Basically I need to be able to compile a Python script without fear of any common leecher to obtain my source code and begin usage of it themselves. Now I know Python is a open source language not meant for being protected but there must be a way?
For example Py2Exe asks you to leave your source code in a plain text file for it then to be compiled. Which is exactly not what I'm wanting. The whole reason for this is that compilation will be happening not just on my own PC. My project is going to be compiled to PE executable form and is intended for public use and I can't have them simply opening and viewing my source.
Can anyone aid me?
You can search for a Python obfuscator or scrumbler but there will be no solution that will protect you intellectual property anyway. If you have developed something completely new go for a patent and try to control it.
In the meantime continue reading at How do I protect python code?.
There's no way to protect anything completely against reverse engineering. Some things you can do:
distribute the .pyc files. This requires slightly more effort to reverse engineer
Use an obfuscator (there aren't a lot of these out there and I haven't found any I would call first class)
Write critical sections (maybe license checks, etc.) in c and call into them
Write critical sections as web services
If you distribute .pyc or py2exe, most people will probably never look at your code. IF someone reverses engineers your code from the byte code, what's the big deal? If they're determined enough, they could just write their own application. Don't worry about it so much and ship your application if it's great enough to be protective of the source.
Well, its not very likely to happen anytime soon - Python simply is an interpreted language that is not readily fit for compilation to native code. What you could do, however, is to use Cython to create a Python extension using Cython's Python-like syntax, compile this to native code and bundle it with a "harmless" application that makes use of your extension module while at the same time making your IP harder to obtain.
You might also try ShedSkin, a native Python to C++ compiler, but you will soon find it very limited.
Still, you might consider if all that is really worth the effort - most of the time it is not.
You could contact the author of Nuitka to see if it could help with distribution of non-readable code.
Edit: Try the Google cache...

Why use Python interactive mode?

When I first started reading about Python, all of the tutorials have you use Python's Interactive Mode. It is difficult to save, write long programs, or edit your existing lines (for me at least). It seems like a far more difficult way of writing Python code than opening up a code.py file and running the interpreter on that file.
python code.py
I am coming from a Java background, so I have ingrained expectations of writing and compiling files for programs. I also know that a feature would not be so prominent in Python documentation if it were not somehow useful. So what am I missing?
Let's see:
If you want to know how something works, you can just try it. There is no need to write up a file. I almost always scratch write my programs in the interpreter before coding them. It's not just for things that you don't know how they work in the programming language. I never remember what the correct arguments to range are to create, for example, [-2, -1, 0, 1]. I don't need to. I just have to fire up the interpreter and try stuff until I figure out it is range(-2, 2) (did that just now, actually).
You can use it as a calculator.
Python is a very introspective programming language. If you want to know anything about an object, you can just do dir(object). If you use IPython, you can even do object.<TAB> and it will tab-complete the methods and attributes of that object. That's way faster than looking stuff up in documentation or even in code.
help(anything) for documentation. It's way faster than any web interface.
Again, you have to use IPython (highly recommended), but you can time stuff. %timeit func1() and %timeit func2() is a common idiom to determine what is faster.
How often have you wanted to write a program to use once, and then never again. The fastest way to do this is to just do it in the Python interpreter. Sure, you have to be careful writing loops or functions (they must have the correct syntax the first time), but most stuff is just line by line, and you can play around with it.
Debugging. You don't need to put selective print statements in code to see what variables are when you write it in the interpreter. You just have to type >>> a, and it will show what a is. Nice again to see if you constructed something correctly. The building Python debugger pdb also uses the intrepeter functionality, so you can not only see what a variable is when debugging, but you can also manipulate or even change it without halting debugging.
When people say that Python is faster to develop in, I guarantee that this is a big part of what they are talking about.
Commenters: anything I am forgetting?
REPL Loops (like Python's interactive mode) provide immediate feedback to the programmer. As such, you can rapidly write and test small pieces of code, and assemble those pieces into a larger program.
You're talking about running Python in the console by simply typing "python"? That's just for little tests and for practicing with the language. It's very useful when learning the language and testing out other modules.
Of course any real software project is written in .py files and later executed by the interpreter!
The Python interpreter is a least common denominator: you can run it on multiple platforms, and it acts the same way (modulo platform-specific modules), so it's pretty easy to get a newbie going with.
It's a lot easier to tell a newbie to launch the interpreter and "do this" than to have them open a file, type in some code, save it, make it executable, make sure python is in your PATH, or use a #! line, etc etc. Scrap all of that and just launch the interpreter. For simple examples, you can't beat it. It was never meant for long programs, so if you were using it for that, you probably missed the part of the tutorial that told you "longer scripts go in a file". :)
you use the interactive interpreter to test snippets of your code before you put them into your script.
As already mentioned, the Python interactive interpreter gives a quick and dirty way to test simple Python functions and/or code snippets.
I personally use the Python shell as a very quick way to perform simple Numerical operations (provided by the math module). I have my environment setup, so that the math module is automatically imported whenever I start a Python shell. In fact, its a good way to "market" Python to non-Pythoniasts. Show them how they can use Python as a neat scientific calculator, and for simple mathematical prototyping.
One thing I use interactive mode for that others haven't mentioned: To see if a module is installed. Just fire up Python and try to import the module; if it dies, then your PYTHONPATH is broke or the module is not installed.
This is a great first step for "Hey, it's not working on my machine" or "Which Python did that get installed in, anyway" bugs.
I find the interactive interpreter very, very good for testing quick code, or to show others the Power of Python. Sometimes I use the interpreter as a handy calculator, too. It's amazing what you can do in a very short amount of time.
Aside from the built-in console, I also have to recommend Pyshell. It has auto-completion, and a decent syntax highlighting. You can also edit multiple lines of code at once. Of course, it's not perfect, but certainly better than the default python console.
When coding in Java, you almost always will have the API open in some browser window. However with the python interpreter, you can always import any module that you are thinking about using and check what it offers. You can also test the behavior of new methods that you are unsure of, to eliminate the "Oh! so THAT's how it works" as a source of bugs.
Interactive mode makes it easy to test code snippets before incorporating them into a larger program. If you use IDLE there's syntax highlighting and argument pop-ups to help you out. It's also a quick way of checking that you've figured out how to use a module without having to write a test program.

What is the feasibility of porting a legacy C program to Python?

I have a program in C that communicates via UDP with another program (in Java) and then does process manipulation (start/stop) based on the UDP pkt exchange.
Now this C program has been legacy and I want to convert it to Python - do you think Python will be a good choice for the tasks mentioned?
Yes, I do think that Python would be a good replacement. I understand that the Twisted Python framework is quite popular.
I'd say that if:
Your C code contains no platform specific requirements
You are sure speed is not going to be an issue going from C to python
You have a desire to not compile anymore
You would like to try utilise exception handling
You want to dabble in OO
You might choose to run on many platforms without porting
You are curious about dynamic typing
You want memory handled for you
You know or want to learn python
Then sure, why not.
There doesn't seem to be any technical reason you shouldn't use python here, so it's a preference in this case.
Remember as well, you can leave parts of your program in C, turn them into Python modules and build python code around them - you don't need to re-write everything up-front.
Assuming that you have control over the environment which this application will run, and that the performance of interpreted language (python) compared to a compiled one (C) can be ignored, I believe Python is a great choice for this.
If I was faced with a similar situation I'd ask myself a couple of questions:
Is there anything more important I could be working on?
Does Python bring anything to the table that is currently handled poorly by the current application?
Will this allow me to add functionality that was previously too difficult to implement?
Is this going to disrupt service in any way?
If I can't answer those satisfactorily, then I'd put off the rewrite.
Yes, I think Python is a good choice, if all your platforms support it. Since this is a network program, I'm assuming the network is your runtime bottleneck? That's likely to still be the case in Python. If you really do need to speed it up, you can include your long-since-debugged, speedy C as Python modules.
If this is an embedded program, then it might be a problem to port it since Python programs typically rely on the Python runtime and library, and those are fairly large. Especially when compared to a C program doing a well-defined task. Of course, it's likely you've already considered that aspect, but I wanted to mention it in the context of the question anyway, since I feel it's an important aspect when doing this type of comparison.

Categories

Resources