IndexError: list index out of range

Posted on 2011-04-20
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\", 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

Question by:dolamitejenkins
    LVL 28

    Expert Comment

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

    Traceback (most recent call last):
      File "", line 15, in <module>
        port = int(sys.argv[1])
    IndexError: list index out of range

    Open in new window


    Author Comment

    thanks pepr... is there some stuff I can read about networking ?
    LVL 28

    Accepted Solution

    I have nothing special to recommend as I do not do the network programming.  I would probably start here (or the frame version, section Applications in the left Content panel, Network programming) to understand the principles.

    Then I would read the "Socket Programming HOWTO" by Gordon McMillan (  

    The details should be searched for namely in the standard doc "17.2. socket — Low-level networking interface" (

    Then you will know enough further questions to search elsewhere (say

    Author Closing Comment

    Thank you

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Oracle connect 6 45
    passing lists 9 39
    run a set of files in Python 2 27
    Update 40,000 records in smaller chunk query - MDB to MSSQL 6 80
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now