Show Menu
Cheatography

Python Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Overview

Interp­reted progra­mming language developed in late 80s inspired by ABC language.
Extens­ibility is one of its major features. Libraries such as Scapy and Requests unlock Python's potential.
Basic Python scripts are fast to write and many libraries support easy creation of HTTP requests, parsing of responses
Many tools are written in Python.
It is widely available and is installed natively on macOS, most Linux distri­but­ions, annd some UNIX systems.
Python is easy to installed, and you can check version with python -v
Python requires consistent indent­ation, using 2 or 4 spaces is common. Tab should be avoided.

Python 2 versus Python 3

Python 2.x is legacy, Python 3.x is the present and future. The final major release of Python 2.7 was in 2010.
Python 2 is still the default version on macOS and Linux, though Python 3 is often included by called 'Python 3'

Python 3 Features

Major improv­ement is better Unicode support, all test strings being Unicode by default
Clean Unicod­e/byte separation
Exception chaining
Function annota­tions
Syntax for keywor­d-only arguments
Extended tuple unpacking
Non-local variable delcar­ations
Other changes include print and exec being statements and integers using floor division.

Data Types and Syntax

String
var="st­rin­g"
Boolean
var=True
Integer
var=86
Float
var=3.1­4159
if/eli­f/else
condit­ional execution of functions
input( )
returns a string by default
int( )
changes a string to an integer
Boolean operators
and, or, not as well as comparison operators ( <, <=, >, >=< ==)
for loops
iterates through a set
while loops
iterates until a condition met

Lists and Dictio­naries

Lists are fundam­ental data structure they contain an ordered list of data.
**list = ['thing1', 'thing2', 'thing3']
Dictio­naries are similar to lists but they are unordered key: value pairs.
**dictionary = {'key': 'value'}
In other languages, dictio­naries are known as associ­ative arrays or hashes.

Web Libraries

urllib
urllib2 - It can perform basic authen­tic­ation, it does not handle underlying details like base-64 encoding or author­ization headers. Python 3 splits functi­onality into urllib.re­quest and urllib.error
urllib3
httplib - Python 3 renamed this http.c­lient
httplib2
Requests developed with a number of PEP 20 idioms in mind
PEP= Python Enhanc­ement Proposals
PEP 20 are "The Zen of Python­"
Requests follows:
1. Beautiful is better than ugly
2. Explicit is better than implicit
3. Simple is better than complex
4. Complex is better than compli­cated
5. Readab­ility counts.
 

Requests

Abstracts many lower-­level details.
Supports multiple authen­tic­ation methods: Basic, Digest, Kerberos, NTLM, AWS, OAuth1
Supports POST with options sent via a dictionary called 'data' in {'vari­able': 'value'}; multiple variables can be passed
Requests can also POST data from a file.
Handles TLS/SSL transp­arently verifying x.509 certif­icates by default (verif­y=True) and will exit if it is invalid. To connec tot a site with an invalid certif­icate by setting verify=False.
r=requ­est­s.g­et(­'ht­tps­://­'in­val­id.c­ert', verify­=False)
print=­(r.t­ext)
Example of Post script:
#! /usr/b­in/­python3
import requests
r=requ­est­s.p­ost­('h­ttp­://­sec­uri­ty.c­om­/fo­rm_­aut­h/l­ogi­n.php', data={­'user': 'admin', 'pass': 'admin', 'button': 'Login'})
print(­r.text)