From Hackerspace Brussels
Jump to: navigation, search

Sat 24 Oct 2009 14:00
till Sat 24 Oct 2009 20:00
Error creating thumbnail: File missing
Python workshop continued
Follow up of the first python workshop
HSB Brussels,Belgium
5 EUR (for non-members)

[[People::User:Fs111| ]]

What is this?[edit]

This is a follow up session on the first Python_Workshop. More to come, watch this space.


Your home work is the following:

  • Build a TCP portscanner that scans a host given on the commandline on all 2^16 TCP ports and prints out all open ports on the given host
  • extension: instead of printing the port number, print the name of the service if it exists in /etc/services


Example session[edit]

This is what I would expect from the scanner:

$ python
open ports on
   22: ssh
   23: telnet
   80: http
 1863: msnp
 1864: paradym-31port
 4443: pharos
 5190: aol
 5431: park-agent
 5566: Unknown service


Put your name here if you are coming:

fs111for great justice!yes
erikloved part 1yes
ptr_missed part 1yes
Antoniomissed the hackerspace
tazoback for moreyes
tvlooyhave you got anything without spam in it?no
nicodachespace blank left is intentionally This
KoertThis space is still left intentionally blankyes
SandbThis intentionally blank space is leftyes

Nifty Tips&tricks[edit]

  • serves your local current working directory
python -m SimpleHTTPServer [port]
  • testing for values in dictionaries
>>> d = {}
>>> 1 in d
>>> d.get(1,"not found")
'not found'
>>> d[1] = "test"
>>> d.get(1,"not found")
>>> 1 in d
  • string replacement, using a dictionary
>>> d = {'name':'pieter', 'age':32}
>>> print "%(name)s %(age)d" % d
pieter 32
  • specify the encoding of the python source file
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
  • play with sets - they are cool (union, intersection, etc)
  • you can use frozenset as index of a dictionary -- (sets are 'unhashable' -- the index value should be 'unchangeable')
 f = frozenset(set([0, 'blah', 'foo', 'bar']))
 d = { f:'some value'}
  • varargs :
def f(*arguments):
  print arguments

def g(**arguments):
  print arguments
  • list comprehension
 l = range(20)
 l2 = [ item for item in l if item %2==0]

or without lists (generator objects)

 l2 = ( item for item in l if item %2==0 )

parsing /etc/services[edit]

i found a nice online utility for creating regex wizardry