user3739941
user3739941

Reputation:

How to send a HTTPS packet in python?

I want to send a HTTPS packed In python. For example I want to send something to 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 receive 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 shall I do to handle it?

Update:

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

enter image description here

But I don't have any idea how and where I must set this parameters in the program.

Update:

Note that I want to extract the used public key in the communication. So please suggest me a way that it make extracting the public key possible. I also want to set or see the used communication and encryption protocols.

Update2:

Although I wanted to set the cipher and ssl_version manually, but based on the answers, I tried to remove this to parameters from my function call and retry to run the program. well, the error 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
>>> 

Upvotes: 3

Views: 1323

Answers (3)

Vaibhav Aggarwal
Vaibhav Aggarwal

Reputation: 1411

Since it looks like you're trying to make simple http requests, you could use the requests library instead, which handles a lot of the stuff for you.

If you don't have it: pip install requests.

import requests

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

Upvotes: 0

adif
adif

Reputation: 55

Why not using requests?

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

That way you will be bound to HTTP

Upvotes: 0

Steffen Ullrich
Steffen Ullrich

Reputation: 123521

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

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

Just omit setting ssl_version and ciphers from your function call. In this case it will offer the server the best protocol version it can and a number of ciphers and the server will pick from these offers what it supports.

This should then make the SSL handshake possible. But since it does not look like your are really trying to talk HTTP on the socket you will probably run into further problems after the connection got established.

Upvotes: 1

Related Questions