PythonWorkshop2

From Hackerspace Brussels
Jump to: navigation, search


PythonWorkshop2
Sat 24 Oct 2009 14:00
till Sat 24 Oct 2009 20:00
Bicycle-repairman1.jpg
What:
Python workshop continued
Tagline:
Follow up of the first python workshop
Where:
HSB Brussels,Belgium
Cost:
5 EUR (for non-members)
Who:
User:Fs111
URL:




[[People::User:Fs111| ]]



Contents

[edit] What is this?

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

[edit] Homework

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


[edit] Tips


[edit] Example session

This is what I would expect from the scanner:

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

[edit] Participants

Put your name here if you are coming:

WhoCommentPresent
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
Xflameyes

[edit] Nifty Tips&tricks

  • serves your local current working directory
python -m SimpleHTTPServer [port]
  • testing for values in dictionaries
>>> d = {}
>>> 1 in d
False
>>> d.get(1,"not found")
'not found'
>>> d[1] = "test"
>>> d.get(1,"not found")
'test'
>>> 1 in d
True
>>> 
  • 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 )

[edit] parsing /etc/services

i found a nice online utility for creating regex wizardry http://txt2re.com/