Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to perform https requests over an SSLsocket in python

Tags:

python

https

ssl

I am using the following code to perform and ssl handshake and certificate validation with an ssl server.

import ssl
import socket

s = socket.socket()
print "connecting..."
#logging.debug("Connecting")
# Connect with SSL mutual authentication
# We only trust our server's CA, and it only trusts user certificates signed by it
c = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED,
                    ssl_version=ssl.PROTOCOL_SSLv3, ca_certs='ca.crt',
                    certfile='user.crt', keyfile='user.key')
c.connect((constants.server_addr, constants.port))

I am able to get a connection to the server and the certificate is validated correctly, however, I am not sure what to do from here. I need to perform https actions over the socket, including posting XML to a REST API. How do I go about this?

like image 375
ewok Avatar asked Mar 28 '26 00:03

ewok


1 Answers

You can use your wrap_socket code to extend httplib.HTTPConnection, as described in this answer.

(I'd still consider using something like PycURL, as I've already answered in your previous question.)

like image 67
Bruno Avatar answered Mar 31 '26 06:03

Bruno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!