• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

IndexError: list index out of range

Im learning networking ... Im using this sample code to start but I keep getting

Traceback (most recent call last):
  File "C:\Python26\Network Code\tmc.py", line 16, in <module>
    port = int(sys.argv[1])
IndexError: list index out of range

Open in new window

Can someone explain what is wrong, how to fix it and why

1 # simple illustration client/server pair; client program sends a string
2 # to server, which echoes it back to the client (in multiple copies),
3 # and the latter prints to the screen
5 # this is the server
7 import socket
8 import sys
10 # create a socket
11 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13 # associate the socket with a port
14 host = ’’ # can leave this blank on the server side
15 port = int(sys.argv[1])
16 s.bind((host, port))
18 # accept "call" from client
19 s.listen(1)
20 conn, addr = s.accept()
21 print ’client is at’, addr
23 # read string from client (assumed here to be so short that one call to
24 # recv() is enough), and make multiple copies (to show the need for the
25 # "while" loop on the client side)
27 data = conn.recv(1000000)
28 data = 10000 * data # concatenate data with itself 999 times
30 # wait for the go-ahead signal from the keyboard (to demonstrate that
31 # recv() at the client will block until server sends)
32 z = raw_input()
34 # now send
35 conn.send(data)
37 # close the connection
38 conn.close()

Open in new window

  • 2
  • 2
1 Solution
Put the "print sys.argv" command just after the line 14.  It seems that there is not enough arguments given when the script was called.  The sys.argv[0] contains the name of the script.  It writes almost exactly the same for me (except the line numbe is actually 15):

c:\tmp\___python\dolamitejenkins\Q_26969336>python tmc.py
Traceback (most recent call last):
  File "tmc.py", line 15, in <module>
    port = int(sys.argv[1])
IndexError: list index out of range

Open in new window

dolamitejenkinsAuthor Commented:
thanks pepr... is there some stuff I can read about networking ?
I have nothing special to recommend as I do not do the network programming.  I would probably start here http://www.alan-g.me.uk/tutor/tutsocket.htm (or the frame version http://www.alan-g.me.uk/tutor/index.htm, section Applications in the left Content panel, Network programming) to understand the principles.

Then I would read the "Socket Programming HOWTO" by Gordon McMillan (http://docs.python.org/howto/sockets.html).  

The details should be searched for namely in the standard doc "17.2. socket — Low-level networking interface" (http://docs.python.org/library/socket.html).

Then you will know enough further questions to search elsewhere (say http://www.google.cz/search?q=python+networking).
dolamitejenkinsAuthor Commented:
Thank you

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now