We help IT Professionals succeed at work.

IndexError: list index out of range

Dolamite Jenkins
on
1,069 Views
Last Modified: 2012-05-11
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
4
5 # this is the server
6
7 import socket
8 import sys
9
10 # create a socket
11 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12
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))
17
18 # accept "call" from client
19 s.listen(1)
20 conn, addr = s.accept()
21 print ’client is at’, addr
22
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)
26
27 data = conn.recv(1000000)
28 data = 10000 * data # concatenate data with itself 999 times
29
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()
33
34 # now send
35 conn.send(data)
36
37 # close the connection
38 conn.close()

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

Author

Commented:
thanks pepr... is there some stuff I can read about networking ?
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.