This is how the response looks like
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 11 Sep 2015 19:18:15 GMT
Expires: Sun, 11 Oct 2015 19:18:15 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
I want to get the index of the empty line, I have tried
response.index("\n\n")
but it does not work.
any suggestions?
thank you!
EDIT: It give me this
Traceback (most recent call last):
File "test.py", line 15, in <module>
test = response.index("\n\n")
ValueError: substring not found
This is my full code:
import socket
server = "www.google.com"
server = socket.gethostbyname(server)
port = 80
request = "GET / HTTP/1.1\nHost: " + server + "\n\n"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, port))
sock.send(request.encode())
response = sock.recv(4096)
print(response)
test = response.index("\n\n")
print(test)
EDIT: After seeing your whole code it was easy to tell that the data received is encoded and you need to decode the data.
The important part was response = sock.recv(4096).decode("utf-8")
which turns a bytes array into a string in python.
This bit of code works for me.
import socket
server = "www.google.com"
server = socket.gethostbyname(server)
port = 80
request = "GET / HTTP/1.1\nHost: " + server + "\n\n"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, port))
sock.send(request.encode())
response = sock.recv(4096).decode("utf-8")
print(response)
test = response.index("\r\n\r")
print(test)