title: Python socket programming - getting a response
body: <p>I'm a newbie to socket programming in python..
  I'm trying to use http head method to get a response from a webserver. In addition, calculating the elapsed time from sending a request until receiving a response.
  in my code i asked for a url and a port number, if the url was correct the html contents of
  that url will be shown on the browser.. and a request message will be displayed on the terminal..
  for example: url=http://www.google.com , port=80,
  the contents of the url &quot;google here&quot; will be shown on localhost:80 ...
  is my code correct? and have i used the head method in the correct way? In addition to the response, i don't get a response message but a request one in the terminal.
  here's my code:</p>
  <pre><code>from socket import *
  import time
  import requests
  serverHost = input(&quot;Enter a URL: &quot;)
  serverPort = input(&quot;Enter a port number: &quot;)
  serverSocket = socket(AF_INET, SOCK_STREAM) #Client socket that uses IPV4 and is of type TCP.
  serverSocket.bind(('', int(serverPort))) #Associate server port number with this socket.
  serverSocket.listen(1) #wait a client
  print (&quot;The server is ready\n&quot;)
  send_time = time.time()
  connectionSocket, addr = serverSocket.accept() #listen for TCP connection requests from the client, connectionSocket is used to send and recieve data on the connection.
  req_mes = connectionSocket.recv(1024).decode() #Request message from the client.
  print (addr) #addr is an address that's bounded to the other end of the connection (CLIENT ADDRESS).
  print (req_mes) #Prints whatever data the client sends and echoes it back
  connectionSocket.send(bytes('HTTP/1.1 200 OK\r\n\r\n', 'UTF-8')) #Send the HTTP response 
  header line to the connection socket
  url = requests.get(serverHost)
  htmltext = url.text
  connectionSocket.send(htmltext.encode()) #encode the message using UTF-8
  connectionSocket.close()
  serverSocket.close()
  recieve_time = time.time()
  print('Time Elapsed:',recieve_time - send_time)
  </code></pre>