'Python send HTTP/S request through socket

I want to send requests only using the socket module. My goal is to get a response with a status code. Anyway, when I send a request through port 443 it responds with nothing

import socket

host = ("www.google.com",80)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(host)

message = '''GET /  HTTP/1.1
Host: www.google.com
Connection: keep-alive



'''
s.send(message.encode())
modifiedMessage = s.recv(2048).decode("ISO-8859-1");
print(modifiedMessage)
s.close()

Output:

HTTP/1.1 200 OK
Date: Sun, 08 Mar 2020 14:35:58 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2020-03-08-14; expires=Tue, 07-Apr-2020 14:35:58 GMT; path=/; domain=.google.com; Secure
Set-Cookie: NID=199=E_sNmu747pC1yNz0-2mc4SbQvL65ABXaSlu2DjgZr8S6U56ie5IMHC6BYOOIbVL9oJ2ydA7mbm0DQhLFNwm7Xw1Q7Z7ABtE8Y6Pd0gMy3Py0-DSMxnzVMtgmkBJAojusMBIGvVMsH_QidWUrqjHrTby2vnyY_7Fx2_pvaCEZ90w; expires=Mon, 07-Sep-2020 14:35:58 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

6974
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="de"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2020/international-womens-day-2020-6753651837108310.2-l.png" itemprop="image"><meta content="Internationaler Frauentag " property="twitter:title"><meta content="Zur Feier des Internationalen Frauentags  #GoogleDoodle #IWD2020" property="twitter:description"><meta content="Zur Feier des Internationalen Frauentags  #GoogleDoodle #IWD2020" property="og:description"><meta content="summary_large_image" property="twitter:card"><meta content="@GoogleDoodles" property="twitter:site"><meta content="https://www.google.com/logos/doodles/2020/international-womens-day-2020-6753651837108310-2xa.gif" property="twitter:image"><meta content="https://www.google.com/logos/doodles/2020/international-womens-day-2020-6753651837108310-2xa.gif" property="og:image"><meta content="818" property="og:image:width"><meta content="460" property="og:image:height"><meta content="https://www.google.com/logos/doodles/2020/international-womens-day-2020-6753651837108310-2xa.gif" property="og:url"><meta content="video.other" property="og:type"><title>Google</title><script nonce="1T1228FRBGn0F7FbqOxhPA==">(function(){window.google={kEI:'zgJlXuORMs

Basically everything works fine but when I change the port from 80 to 443 I just get an empty repsone



Solution 1:[1]

You need to use the tools provided in the ssl module, like in this example code.

import socket
import ssl


message = """GET /  HTTP/1.1
Host: www.google.com
Connection: keep-alive



"""

host = ("www.google.com", 443)
purpose = ssl.Purpose.SERVER_AUTH
context = ssl.create_default_context(purpose=purpose)

with socket.create_connection(host) as sock:
    with context.wrap_socket(sock, server_hostname=host[0]) as wrapped:

        wrapped.send(message.encode())
        modifiedMessage = wrapped.recv(2048).decode("ISO-8859-1")
        print(modifiedMessage)

If you plan to do this in production code you should review the warnings and advice in the ssl module documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1