pythonurllibhttplib

sending http requests with specific/non-existent http version protocol in Python


There is some way to send http requests in python with specific http version protocol.I think that, with httplib or urllib, it is not possible.

For example: GET / HTTP/6.9

Thanks in advance.


Solution

  • The simple answer to your question is: You're right, neither httplib nor urllib has public, built-in functionality to do this. (Also, you really shouldn't be using urllib for most things—in particular, for urlopen.)

    Of course you can always rely on implementation details of those modules, as in Lukas Graf's answer.

    Or, alternatively, you could fork one of those modules and modify it, which guarantees that your code will work on other Python 2.x implementations.*. Note that httplib is one of those modules that has a link to the source up at the top, which means it's mean to server as example code, not just as a black-box library.

    Or you could just reimplement the lowest-level function that needs to be hooked but that's publicly documented. For httplib, I believe that's httplib.HTTPConnection.putrequest, which is a few hundred lines long.

    Or you could pick a different library that has more hooks in it, so you have less to hook.

    But really, if you're trying to craft a custom request to manually fingerprint the results, why are you using an HTTP library at all? Why not just do this?

    msg = 'GET / HTTP/6.9\r\n\r\n'
    s = socket.create_connection((host, 80))
    with closing(s):
        s.send(msg)
        buf = ''.join(iter(partial(s.recv, 4096), ''))
    

    * That's not much of a benefit, given that there will never be a 2.8, all of the existing major 2.7 implementations share the same source for this module, and it's not likely any new 2.x implementation will be any different. And if you go to 3.x, httplib has been reorganized and renamed, while urllib has been removed entirely, so you'll already have bigger changes to worry about.