I am completely new in Python.I need to write a python script which parse the request url of apache and find some data out of it.
Script is:
#!/usr/bin/python
import sys
import re
for line in sys.stdin:
ipAddress = re.search('ipAddress=([^&]*)', line)
key = re.search('key=([^&]*)', line)
if len(ipAddress.group(1)) != 0 and len(key.group(1)) != 0:
print "%s\t%s" % (ipAddress.group(1), key.group(1))
Please tell me what is the error in the script and how to test it with input like this:
GET request?key=xxxxxxxxxx&ipAddress=000.000.000.00&id=blah... HTTP/1.1
You haven't specified what error you're getting, and your code doesn't generate any error for me, but I can see a few things that could go bad with your code.
First, you just want the URL, not the method and HTTP version (which could cause some problems if ipAddress or key were the last fields), so you should split the line before parsing it:
method, url, version = line.split(' ')
Second, you will get errors if the url doesn't contain either a key
or ipAddress
field, because your match checking code is faulty. You should use this code instead:
for line in sys.stdin:
method, url, version = line.split(' ')
ipAddress = re.search('ipAddress=([^&]*)', url)
key = re.search('key=([^&]*)', url)
if ipAddress and key:
print "%s\t%s" % (ipAddress.group(1), key.group(1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.