Making multiple SSL requests fast in Python 2.x

280 views
Skip to first unread message

Ram Rachum

unread,
Apr 3, 2012, 5:10:50 PM4/3/12
to pyweb-il
Hey guys,

I have a question about SSL connections in Python.

My Django app does a lot of HTTPS communication with a third-party server in the US. That is the Braintree server. Braintree is a nice service for processing credit cards. (It's the one that inspired the talk about never missing a request that we had in our last meeting.)

Every time my app wants to communicate with Braintree, it starts a new HTTPS request.

I came to understand that this is very inefficient, because then the SSL handshake needs to be made every single time, which is taking up a lot of roundtrips, which are very expensive because of how far away we are from the server. I used Wireshark to measure it, and learned that the SSL handshake is taking 40% of the communication time.

My question: Is there a way to make subsequent HTTPS requests fast after the first one was made?

(I don't know much about SSL, and I don't know whether this means keeping connections alive, or any other thing. I'm open to suggestions.)

(One thing I saw that could have helped is `ssl.SSLContext`, but it's only available in Python 3.2, while we're working with Python 2.7.)



Thanks for your help,
Ram.

oz katz

unread,
Apr 3, 2012, 6:33:43 PM4/3/12
to pywe...@googlegroups.com
Hi,

I'm not familiar with the BrainTree API, bug in general for HTTP\HTTPS you can utilize urllib3 (https://github.com/shazow/urllib3) or Requests (http://docs.python-requests.org/en/latest/index.html) which is built on top of urllib3 which offer connection pooling for HTTP and HTTPS.

You can read a bit more about it here: http://urllib3.readthedocs.org/en/latest/pools.html

Both these packages are under active development and are well maintained, so I'm betting you will find what you need there.

Thanks,
Oz


--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To post to this group, send email to pywe...@googlegroups.com.
To unsubscribe from this group, send email to pyweb-il+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyweb-il?hl=en.

Ram Rachum

unread,
Apr 3, 2012, 6:38:59 PM4/3/12
to pywe...@googlegroups.com
Thanks Oz!

I've actually just spent the last hour monkeypatching the `braintree` package to use `requests` internally so I could use all its goodies.

I was surprised and very pleased with how easy it was to make `braintree` use `requests`. It's already working!

But what I'm still frustrated with is that I haven't been able to make it reuse the SSL connections and thus speed up my app. By using `requests`'s `keep_alive` setting I think I managed to make it reuse the TCP connection (or is it HTTP? I'm a novice at networking) but it's definitely not reusing the SSL. I can see in the packet sniffer that it does the SSL handshake over and over again.

Any idea how to reuse the SSL connection?

Uriel Katz

unread,
Apr 3, 2012, 6:53:41 PM4/3/12
to pywe...@googlegroups.com

SSL is a layer on top TCP and HTTP is a layer(in case of https) on top SSL.
Do you see Connection: close in the response headers if so then the server doesn't want to keep the connection alive.

Ram Rachum

unread,
Apr 3, 2012, 7:03:36 PM4/3/12
to pywe...@googlegroups.com
Thanks for your help Uriel.

Did you mean in the HTTP response? Yes, I am seeing `'connection':  'close'`.

So... Do you conclude from that that the Braintree server doesn't want to allow me to reuse the connection?

So... There is no hope that I'll be able to reuse the connection?

Also, does this refer to the TCP connection or the SSL connection?

Uriel Katz

unread,
Apr 3, 2012, 7:07:11 PM4/3/12
to pywe...@googlegroups.com

Nope,if they close it you can't reuse it(you can try in case they only tell the client to close it but they don't really close it but that is hacky).

Check if they have a way to batch requests in their API

Ram Rachum

unread,
Apr 4, 2012, 5:19:04 PM4/4/12
to pywe...@googlegroups.com
I understand.

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages