PythonWorkshop2
From Hackerspace Brussels
|
[[People::User:Fs111| ]]
Contents
What is this?[edit]
This is a follow up session on the first Python_Workshop. More to come, watch this space.
Homework[edit]
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
Tips[edit]
Example session[edit]
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
Participants[edit]
Put your name here if you are coming:
Who | Comment | Present |
---|---|---|
fs111 | for great justice! | yes |
erik | loved part 1 | yes |
ptr_ | missed part 1 | yes |
Antonio | missed the hackerspace | |
tazo | back for more | yes |
tvlooy | have you got anything without spam in it? | no |
nicodache | space blank left is intentionally This | |
Koert | This space is still left intentionally blank | yes |
Sandb | This intentionally blank space is left | yes |
Xflame | yes |
Nifty Tips&tricks[edit]
- 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 )
parsing /etc/services[edit]
i found a nice online utility for creating regex wizardry http://txt2re.com/