Best way to know if http request is GET or POST with DPKT?
I'm using the function dpkt.http.Request(), but sometimes the http flow is not a request. Is there a quick way in python 开发者_开发知识库or dpkt to know if my request is GET or POST?
Try parsing it as a HTTP request and catch dpkt.UnpackError
so your program doesn't die if it's not a HTTP request.
If no exception was thrown, you can use .method
of the Request
object to get the method that was used.
>>> import dpkt
>>> r = dpkt.Request('GET / HTTP/1.0\r\n\r\n')
Finding the HTTP verb is easy:
>>> r.method
"GET"
If there is difficulty parsing the HTTP request data, then it is impossible to get the original source data back because of how the dpkt.Request.unpack
method handles exceptions.
As a workaround something like this may be handy:
>>> data = 'GET / HTT' #malformed request
>>> try:
... r = dpkt.Request(data)
... except dpkt.UnpackError:
... print data.split()[0]
...
'GET'
精彩评论