Socket.error: [Errno 61] Connection refused Python
<p>I want to write a HTTP client to test the web server. The client will use TCP connection.</p>

<pre><code>#Import socket module
from socket import *
import sys
server_host = sys.argv[1]
server_port = int(sys.argv[2])
filename = sys.argv[3]

clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((server_host,server_port))

while True:
    print 'Ready to server...'
    connectionSocket, addr = clientSocket.accept()
    print 'connected from', addr
    try:
        f = open(filename[1:])
        outputdata = f.read()
        #Send one HTTP header line into socket
        connectionSocket.sendall('\nHTTP/1.1 200 OK\r\n\r\n')
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):  
            connectionSocket.sendall(outputdata[i])
        #Closes the socket for this client
        connectionSocket.close()
        print 'File Received'
    except IOError:
        print 'IOError'
        #Send response message for file not found
        connectionSocket.sendall('\n404 Not Found\n') 
        #Close client socket 
        connectionSocket.close()
clientSocket.close()
</code></pre>

<p>Command lind input is:</p>

<pre><code>python  ./client.py  127.0.0.1  8000  HelloWorld.html
</code></pre>

<p>This script outputs :</p>

<pre><code>Traceback (most recent call last):File "./client.py", line 9, in &lt;module&gt;
clientSocket.connect((server_host,server_port))
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
</code></pre>

<p>I'm running Python 2.7.11 on Mac OS X.</p>

<p>I have tried searching around a lot haven't got a solution.</p>