title: error: [Errno 10054] An existing connection was forcibly closed by the remote host
body: <p>I am new in socket programming,could someone please tell me what is wrong I am doing here.I am getting this error.This program works fine when I am not calling defining it as a function.The moment I called it from main it is giving me this error.Someone please help!</p>
  
  <pre><code>#!/usr/bin/env python
  import socket
  import sys
  import urlparse
  from urlparse import urlparse
  
  def main(url):
    t=url
    o = urlparse(t)
    x=".".join(o.netloc.split(".")[-2:])
    head1= o.path
    host = o.netloc
    port = 80
  
    try:
       sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, msg:
       sys.stderr.write("[ERROR] %s\n" % msg[1])
       sys.exit()
    try:
       sock.connect((host, port))
       except socket.error, msg:
       sys.stderr.write("[ERROR] %s\n" % msg[1])
       sys.exit()
    try:
       sock.send("HEAD %s HTTP/1.0\r\n\r\n" % head1)
    except sock.error:
       sys.exit()   
  
    s=sock.recv(600)
    sock.shutdown()
    import sys
    sys.exit
    sock.close()
    return s
  
   x=main("www.google.ca")
   print x
  
  ERROR I get:
  Traceback (most recent call last):
  File "C:/myserver/cgi-bin/domainsockettest.py", line 47, in &lt;module&gt;
  x=main("www.google.ca")
  File "C:/myserver/cgi-bin/domainsockettest.py", line 40, in main
  s=sock.recv(600)
  error: [Errno 10054] An existing connection was forcibly closed by the remote host
  </code></pre>