pyparsing: how to negate a grammar

knguyen at megisto.com knguyen at megisto.com
Sun Jan 9 09:27:09 EST 2005


Hi Paul,

I am trying to extract HTTP response codes  from a HTTP page send from
a web server.  Below is my test program. The program just hangs.

Thanks,
Khoa
##################################################

#!/usr/bin/python

from pyparsing import   ParseException, Dict, CharsNotIn,
Group,Literal,Word,ZeroOrMore,OneOrMore,
Suppress,nums,alphas,alphanums,printables,restOfLine


data = """HTTP/1.1 200 OK
body line some text here
body line some text here
HTTP/1.1 400 Bad request
body line some text here
body line some text here

HTTP/1.1 500 Bad request
body line some text here
body line some text here
"""

print "================="
print data
print "================="

HTTPVersion = (Literal("HTTP/1.1")).setResultsName("HTTPVersion")
StatusCode = (Word(nums)).setResultsName("StatusCode")
ReasonPhrase = restOfLine.setResultsName("ReasonPhrase")
StatusLine = Group(HTTPVersion + StatusCode + ReasonPhrase)

nonHTTP = ~Literal("HTTP/1.1")
BodyLine = Group(nonHTTP + restOfLine)
Response = OneOrMore(StatusLine + ZeroOrMore(BodyLine))
respFields = Response.parseString(data)
print respFields




More information about the Python-list mailing list