regex - how to run and check run in python -


i new in python.i need write python script parse request url of apache , find data out of it.

script :

#!/usr/bin/python  import sys import re  line in sys.stdin:     ipaddress = re.search('ipaddress=([^&]*)', line)     key = re.search('key=([^&]*)', line)     if len(ipaddress.group(1)) != 0 , len(key.group(1)) != 0:         print "%s\t%s" % (ipaddress.group(1), key.group(1)) 

please tell me error in script , how test input this:

get request?key=xxxxxxxxxx&ipaddress=000.000.000.00&id=blah... http/1.1 

you haven't specified error you're getting, , code doesn't generate error me, can see few things go bad code.

first, want url, not method , http version (which cause problems if ipaddress or key last fields), should split line before parsing it:

method, url, version = line.split(' ') 

second, will errors if url doesn't contain either key or ipaddress field, because match checking code faulty. should use code instead:

for line in sys.stdin:     method, url, version = line.split(' ')     ipaddress = re.search('ipaddress=([^&]*)', url)     key = re.search('key=([^&]*)', url)     if ipaddress , key:         print "%s\t%s" % (ipaddress.group(1), key.group(1)) 

Comments