How do I send an HTTPS packet in python?

I want to send HTTPS in Python. For example, I want to send something to a google server. I wrote the following program:

import socket
import ssl

# SET VARIABLES
packet, reply = "<packet>SOME_DATA</packet>", ""
HOST, PORT = 'www.google.com', 443

# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)

# WRAP SOCKET
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")

# CONNECT AND PRINT REPLY
wrappedSocket.connect((HOST, PORT))
wrappedSocket.send(packet)
print wrappedSocket.recv(1280)

# CLOSE SOCKET CONNECTION
wrappedSocket.close()

      

But when I run it I get the following error:

>>> ================================ RESTART ================================
>>> 

Traceback (most recent call last):
  File "C:/Users/Desktop/PySSL.py", line 16, in <module>
    wrappedSocket.connect((HOST, PORT))
  File "C:\Python27\lib\ssl.py", line 297, in connect
    self.do_handshake()
  File "C:\Python27\lib\ssl.py", line 281, in do_handshake
    self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:499: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
>>> 

      

What should I do to deal with this?

Update:

I have already checked Google's encryption protocol and you can see the result in the following picture:

enter image description here

But I don't know how and where I should set these parameters in the program.

Update:

Please note that I want to extract the used key into the post. So please suggest me a way so that it can extract the public key. I also want to install or view the communication and encryption protocols used.

Update2:

Although I wanted to install cipher

and ssl_version

manually, but based on the answers, I tried to remove this from the parameters from my function call and try again to run the program. well, the error has changed:

>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "C:/Users/Desktop/PySSL2.py", line 17, in <module>
    wrappedSocket.send(packet)
  File "D:\Python34\lib\ssl.py", line 679, in send
    v = self._sslobj.write(data)
TypeError: 'str' does not support the buffer interface
>>> 

      

+3


source to share


3 answers


wrappedSocket = ssl.wrap_socket (sock, ssl_version = ssl.PROTOCOL_TLSv1, ciphers = "ADH-AES256-SHA")

You are trying to use ADH, i.e. anonymous authentication cipher. This is a cipher that does not use certificates for authentication and is generally only supported by misconfigured servers.



Just omit the ssl_version parameter and ciphers from your function call. In this case, he will offer the server the best version of the protocol that he can, and the cipher suite and the server will take away from these proposals what it supports.

This should then make SSL handshaking possible. But since this doesn't sound like you are actually trying to speak HTTP on a socket, you are likely to run into additional problems after the connection is established.

+1


source


Why not use queries?

import requests
requests.get('https://www.google.com', port=443)

      



This way you will be linked to HTTP

0


source


Since it looks like you are trying to make simple HTTP requests, you can instead use a library requests

that handles a lot of things for you.

If you do not have: pip install requests

.

import requests

resp = requests.get("http://www.google.com")
print resp.text

      

0


source







All Articles