2

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?

asked Jun 16, 2019 at 12:18

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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
Is there any resource over internet where I can read issuing HTTPS requests?
you can try using Pythons ssl`: docs.python.org/3/library/ssl.html . so that it takes for the S part for you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.