Monday, September 21, 2009

Python: Intro, cytpes, farts

So past couple of weeks I've been working on Python trying to learn this language. In fact I tried it a few years ago but I got distracted and put it on the back burner. So currently I am back on track giving this another attempt. Here is my disclaimer: I am a Python novice. Anything I say for the time being regarding Python are just my experiences and for documentation purposes. As far as facts, I am making the best efforts to try to make everything informative and accurate.

The reason I'm choosing Python treads along the lines of it being easy to read the syntax and in my opinion, seems to be a nice stone-stepper language (among the common reasons: exploits, security, etc). It also has a nice library set and can produce language that is compatible with C.

For instance, we could easily use a CTYPE module in Python to call functions in DLLs. Using the method cdll() to call functions in the C runtime DLL, MSVCRT.DLL. Namely, Microsoft Visual C++ Runtime. This is obviously a Microsoft library with C++ runtimes. I didn't know that specifically before.

So from what I gather, the C++ library just mentioned, can be used as sort of a reference for your Python scripts, to "call" functions within that library that will assist you in carrying out whatever it is you are trying to do, without have to use a C++ programming language.

For example, below is some code (novice) that does just that:
--------------
from ctypes import *

msvcrt.dll = cdll.msvcrt
message_string = "Hello world!\n"
msvcrt.printf("Testing: %s", message_string)
--------------
What this should do for you is output the text, "Hello world!" Pretty easy no? If you read the code, you can notice how you are telling the script to use the CTYPE module and to call the printf (in the C runtime) function using the cdll() method to output the words inside the first set of quotes. A similar thing can be done with this simple line of code just using Python alone:

--------------
>>> print "Hello world!"
Hello world!
--------------
The difference was, the first example was to show that you can easily access the C runtime library to use its functions to accomplish tasks with Python. What this seems to implicate thus far is that instead of just being limited to this small Python language, you actually expand you resources and arsenal by using other libraries that already exist in the operating system. If you think about it, that is a powerful thing, sploitz anyone?

Now considering how new I am to higher programming languages in general, I did make a mistake with the above code in example 1. After checking spelling and syntax over and over I was about to pull my hair out before learning a valuable lesson. It is case-sensitive. Of course I knew that, but it completely eluded me and one hour later, I did a face palm.

The reason the code was not running was due to a very simple mistake. It was in the line, "from ctypes import *". The F was capitalized and was causing the program not to "expect" the  command as it literally informed me of. Humorously, it attempted to tell me what it did "expect." Something to the lines of, Python was "expecting //, if, or," etc. It gave me a list of commands and functions (a long one) in trying to tell me I was wrong. After figuring it out I felt I accomplished something even though it was my mistake, what better way can you learn than from your own mistakes?

Sorry if this post felt straight to the point, I am out of time today and leaving now. Anyway, this was just a short binary fart that the wonders of autonomous pressure releases of an admin learning a language results in. Did that even make sense? Probably not.


- - -
Vocabulary


ctype: Python modules that allow you to own


cdll(): Loads libraries using the cdecl calling convention


binary fart: invented by adminalive.blogspot.com, Copyright 2009 all rights reserved. (still figuring out what it exactly means >:-\) Usage: "I had a long binary fart that ones and zeros fell out my ass crack." OR "I barted."

No comments:

Post a Comment