user420814
user420814

Reputation: 203

How can I make an http request without getting back an http response in Python?

I want to send it and forget it. The http rest service call I'm making takes a few seconds to respond. The goal is to avoid waiting those few seconds before more code can execute. I'd rather not use python threads I'll use twisted async calls if I must and ignore the response.

Upvotes: 6

Views: 2839

Answers (3)

chx
chx

Reputation: 11790

You do not need twisted for this, just urllib will do. See http://pythonquirks.blogspot.com/2009/12/asynchronous-http-request.html

I am copying the relevant code here but the credit goes to that link:

import urllib2

class MyHandler(urllib2.HTTPHandler):
    def http_response(self, req, response):
        return response

o = urllib2.build_opener(MyHandler())
o.open('http://www.google.com/')

Upvotes: 0

Josh K
Josh K

Reputation: 28893

You are going to have to implement that asynchronously as HTTP protocol states you have a request and a reply.

Another option would be to work directly with the socket, bypassing any pre-built module. This would allow you to violate protocol and write your own bit that ignores any responses, in essence dropping the connection after it has made the request.

Upvotes: 1

thelost
thelost

Reputation: 6694

HTTP implies a request and a reply for that request. Go with an async approach.

Upvotes: 0

Related Questions