import socket
import sys
# creating socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'www.google.com'
port = "80"
x = "GET  /  HTTP/1.1\r\nHost :  "+ host + "\n\n"
s.connect((host, port)
s.send(request.encode()) # the line has error invalid syntax 
response  = s.recv(4096)
print (response)
Python (qpython) socket programming
469 Views Asked by Jaydeep Ranpariya At
        	2
        	
        There are 2 best solutions below
0
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                Adding to the existing answer about the problems with your code:
x = "GET / HTTP/1.1\r\nHost : "+ host + "\n\n"
You are trying to construct a HTTP request here but get it wrong:
- there should be no space after Host, i.e. it should beHost: hostnameand notHost: hostnameas you do.
- The end of line should be \r\nand not\n. You get it right after the first line but not after theHostheader and not for the end-of-header marker.
Error in this line:
Reason: you have defined request with variable name
xso this should work for you,