Skip to content

Instantly share code, notes, and snippets.

@kaskavalci
Created September 26, 2015 10:26
Show Gist options
  • Save kaskavalci/aace72b5336cf7330249 to your computer and use it in GitHub Desktop.
Save kaskavalci/aace72b5336cf7330249 to your computer and use it in GitHub Desktop.
httpRequest = """
GET /index.html HTTP/1.1
Accept-Encoding: identity
Host: #Your IP Address
Connection: close
User-Agent: Python
"""
try:
#Create the socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Set timeout to some seconds, 2 in this case. Don't wait forever if server doesn't respond.
s.settimeout(2)
#Connect to your host
s.connect(("google.com",80))
#Send HTTP request
s.send(httpRequest)
#Get the response
data = (s.recv(4096))
#Enjoy your response!
print data
#Close the socket when done
s.close()
except Exception, e:
print "Error communicating with host.", sys.exc_info()[0]
sys.exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment