Book Image

Python for Secret Agents

By : Steven F. Lott, Steven F. Lott
Book Image

Python for Secret Agents

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (12 chapters)
Python for Secret Agents
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Confirming our tools


To be sure we have a working Python tool, it's best to check things from the command prompt. We're going to do a lot of our work using the command prompt. It involves the least overhead and is the most direct connection to Python.

The Python 3.3 program shows a startup message that looks like this:

MacBookPro-SLott:Secret Agent's Python slott$ python3
Python 3.3.4 (v3.3.4:7ff62415e426, Feb  9 2014, 00:29:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type help, copyright, credits or license for more information.
>>>

We've shown the operating system's prompt (MacBookPro-SLott:Secret Agent's Python slott$), the command we entered (python3), and the response from Python.

Python provides three lines of introduction followed by its own >>> prompt. The first line shows that it's Python 3.3.4. The second line shows the tools used to build Python (GCC 4.2.1). The third line provides some hints about things we might do next.

We've interacted with Python. Things are working.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Feel free to enter copyright, credits, and license at the >>> prompt. They may be boring, but they serve as confirmation that things are working.

It's important to note that these objects (copyright, credits, and license) are not commands or verbs in the Python language. They're global variables that were created as a convenience for first-time Python agents. When evaluated, they display blocks of text.

There are two other startup objects we'll use: exit and help. These provide little messages that remind us to use the help() and exit() functions.

How do we stop?

We can always enter exit to get a reminder on how to exit from interactive Python, as follows:

>>> exit

Use exit() or Ctrl + D (that is EOF (end-of-file)) to exit.

Windows agents will see that they must use Ctrl + Z and Return to exit.

Python is a programming language that also has an interactive prompt of >>>. To confirm that Python is working, we're responding to that prompt, using a feature called the Read-Execute-Print Loop (REPL).

In the long run, we'll be writing scripts to process our data. Our data might be an image or a secret message. The end of a script file will exit from Python. This is the same as pressing Ctrl + D (or Ctrl + Z and Return) to send the EOF sequence.

Using the help() system

Python has a help mode, which is started with the help() function. The help() function provides help on a specific topic. Almost anything you see in Python can be the subject of help.

For pieces of Python syntax, such as the + operator, you'll need to use a string meaning you should enclose the syntax in quotes. For example, help("+") will provide detailed help on operator precedence.

For other objects (such as numbers, strings, functions, classes, and modules) you can simply ask for help on the object itself; quotes aren't used. Python will locate the class of the object and provide help on the class.

For example, help(3) will provide a lot of detailed, technical help on integers, as shown in the following snippet:

>>> help(3)
Help on int object:
class int(object)
|  int(x=0) -> integer
|  int(x, base=10) -> integer
|
etc.

When using the help() module from the command line, the output will be presented in pages. At the end of the first page of output, we see a new kind of non-Python prompt. This is usually :, but on Windows it may be -- More --.

Python normally prompts us with >>> or .... A non-Python prompt must come from one of the help viewers.

Mac OS and GNU/Linux secrets

In POSIX-compatible OSes, we'll be interacting with a program named less; it will prompt with : for all but the last page of your document. For the last page, it will prompt with (END).

This program is very sophisticated; you can read more about it on Wikipedia at http://en.wikipedia.org/wiki/Less_(Unix).

The four most important commands are as follows:

  • q: This command is used to quit the less help viewer

  • h: This command is used to get help on all the commands that are available

  • ˽: This command is used to enter a space to see the next page

  • b: This command is used to go back one page

Windows secrets

In Windows, we'll usually interact with a program named more; it will prompt you with -- More --. You can read more about it on Wikipedia from http://en.wikipedia.org/wiki/More_(command).

The three important commands here are: q, h, and ˽.

Using the help mode

When we enter help() with no object or string value, we wind up in help mode. This uses Python's help> prompt to make it very clear that we're getting help, not entering Python statements. To go back to ordinary Python programming mode, and enter quit.

The prompt then changes back to >>> to confirm that we can go back to entering code.

Your next training mission is to experiment with the help() function and help mode before we can move on.