Search code examples
pythonsocketsssl

SSL Socket Forcibly Closed - Python & IoT


I have an IoT device that successfully communicates with the Google APIs. I'm trying to get it to communicate with a new server, but with no luck. On the IoT side, I would get a NULL response after about 7 or 8 seconds. I think I understand this to mean the socket was closed but am not sure why.

I have a simple python script running under Windows that seems to explain more. It appears that Windows is closing the socket. Maybe this is a clue as to why my IoT is also failing. (The socket firmware is 3rd party and I don't have an understanding of what's inside.)

Here is the script:

import socket
import ssl

# Host and port
host = '<deleted>'
port = 443  # HTTPS port

# Create a TCP socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("\r\nsocket created", host, "\r\n")

# Wrap the TCP socket with SSL
ssl_socket = ssl.wrap_socket(tcp_socket, ssl_version=ssl.PROTOCOL_TLS)

print("TCP converted to SSL socket", "\r\n")

# Connect to the server
ssl_socket.connect((host, port))

print("connection established", host, port, "\r\n")

print("execute send command:", "ssl_socket.send(b\"GET https://<deleted>\")")

ssl_socket.send(b"GET https://<deleted>")

print("\r\n\r\nresponse:")

response = ssl_socket.recv(4096)
print(response.decode())

ssl_socket.close()

And here is the output I get when running under Windows:

C:\Python37>python server.py

socket created <deleted>

TCP converted to SSL socket

connection established <hidden> 443

execute send command: ssl_socket.send(b"GET https://<deleted>")


response:
Traceback (most recent call last):
  File "server.py", line 29, in <module>
    response = ssl_socket.recv(4096)
  File "C:\Python37\lib\ssl.py", line 1034, in recv
    return self.read(buflen)
  File "C:\Python37\lib\ssl.py", line 910, in read
    return self._sslobj.read(len)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

C:\Python37>

One thing I'm totally lost on is WRT certificates. I've been told by the server engineers that I'm using the correct certificate and it appears to be installed properly in my IoT. Is it possible the socket is being closed because the certificate isn't accepted? I'm guessing not since the socket is opened successfully, so it must be something else.

One last thing... The server has a sandbox that I use in a browser. I can successfully communicate with the server using the same Windows machine that I'm running python on.

I have both the company responsible for the IoT socket stack and the company with the server looking into this (for about 6 weeks now). I'm hoping someone can give me suggestions on how to resolve this.

Thanks for your time reading my long post.

Dave


Solution

  • In the end, it was NOT:

    1. incorrect HTTP
    2. invalid GET
    3. invalid GET
    4. invalid character in the key

    It was:

    The host company had never worked with an IoT before and were unaware of required certificates that are normally taken care of by your PC.