#!/usr/bin/env python
import httplib
import sys
#get http server ip
http_server = sys.argv[0]
#create a connection
conn = httplib.HTTPConnection(http_server)
while 1:
cmd = raw_input('input command (ex. GET index.html): ')
cmd = cmd.split()
if cmd[0] == 'exit': #type exit to end it
break
#request command to server
conn.request(cmd[0],cmd[1])
#get response from server
rsp = conn.getresponse()
#print server response and data
print(rsp.status, rsp.reason)
data_received = rsp.read()
print(data_received)
conn.close()
Error
Traceback (most recent call last):
File "./client1.py", line 19, in <module>
conn.request(cmd[0],cmd[1])
IndexError: list index out of range
can any one tell me why that error is coming and can anyone modify the code. it is a client side code to connect with server
my input is :GET index.html
But now my error is
File "./client1.py", line 19, in <module>
conn.request(cmd[0],cmd[1])
File "/usr/lib/python2.6/httplib.py", line 910, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.6/httplib.py", line 947, in _send_request
self.endheaders()
File "/usr/lib/python2.6/httplib.py", line 904, in endheaders
self._send_output()
File "/usr/lib/python2.6/httplib.py", line 776, in _send_output
self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 735, in send
self.connect()
File "/usr/lib/python2.6/httplib.py", line 716, in connect
self.timeout)
File "/usr/lib/python2.6/socket.py", line 500, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
What are you providing as input to the question?
'input command (ex. GET index.html): '
The code expects you to input a command like
GET index.html
it then splits that into an array called cmd, Later it tries access index 0 and 1 of thecmd
array and it finds that there is no cmd[1] or no cmd[0] and cmd[1] probably because of the input it received.EDIT
In answer to your second question you need to change your line
http_server = sys.argv[0]
to
http_server = sys.argv[1]
You then run your script like
./scriptname www.google.co.uk