I am trying to issue a simple HTTP GET request to a website through Python socket library. The sample website I used here is https://azlyrics.com/lyrics/charlieputh/attention.html. My code is:
from socket import *
serverName = 'azlyrics.com';
serverPort = 80;
clientSocket = socket(AF_INET, SOCK_STREAM);
print(clientSocket.connect((serverName, serverPort)));
message = '''GET /lyrics/charlieputh/attention.html HTTP/1.1
Host: www.azlyrics.com
Connection: keep-alive
''';
print(message);
clientSocket.send(message.encode());
modifiedMessage = clientSocket.recv(2048).decode();
print(modifiedMessage);
clientSocket.close();
But I get no response message in return. Also the object I get in return of connect() is None. I have tried the same URL with Python Request Library and it works fine. What am I doing wrong here?
1 Answer 1
HTTP requests body should end with \r\n (CR-LF),
please try this:
from socket import *
serverName = 'azlyrics.com';
serverPort = 80;
clientSocket = socket(AF_INET, SOCK_STREAM);
print(clientSocket.connect((serverName, serverPort)));
message = '''GET /lyrics/charlieputh/attention.html HTTP/1.1
Host: www.azlyrics.com
Connection: keep-alive
''';
print(message);
clientSocket.send(message.encode());
modifiedMessage = clientSocket.recv(2048).decode();
print(modifiedMessage);
clientSocket.close();
answered Jun 16, 2019 at 12:44
Adam.Er8
13.5k3 gold badges32 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Usman Mani
I dont know WTF is wrong with me? How did I missed that simple thing... Anyway thanks a lot you saved me a week of headache
Usman Mani
Is there any resource over internet where I can read issuing HTTPS requests?
Adam.Er8
you can try using Python
s ssl`: docs.python.org/3/library/ssl.html . so that it takes for the S part for you.lang-py