Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

sys.argv[1] IndexError: list index out of range

Posted on 2011-05-07
3
Medium Priority
?
3,016 Views
Last Modified: 2012-05-11

client side server keeps giving me
Traceback (most recent call last):
  File "C:\Python26\Network Folder\client_side.py", line 8, in <module>
    FILE = sys.argv[1]
IndexError: list index out of range

Open in new window

I dont know why or what it mean or how to correct it
 2nd I am trying to transfer a file ie ( thisdbfile.sqlite) how can I do that using this code
# USAGE: python FileSender.py [file]

import sys, socket

HOST = 'localhost'
CPORT = 9091
MPORT = 9090
FILE = sys.argv[1]

cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cs.connect((HOST, CPORT))
cs.send("SEND " + FILE)
cs.close()

ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ms.connect((HOST, MPORT))

f = open(FILE, "rb")
data = f.read()
f.close()

ms.send(data)
ms.close()

Open in new window


# USAGE: python FileReciever.py

import socket, time, string, sys, urlparse
from threading import *

#------------------------------------------------------------------------

class StreamHandler ( Thread ):

    def __init__( this ):
        Thread.__init__( this )

    def run(this):
        this.process()

    def bindmsock( this ):
        this.msock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        this.msock.bind(('', 9090))
        this.msock.listen(1)
        print '[Media] Listening on port 9090'

    def acceptmsock( this ):
        this.mconn, this.maddr = this.msock.accept()
        print '[Media] Got connection from', this.maddr
    
    def bindcsock( this ):
        this.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        this.csock.bind(('', 9091))
        this.csock.listen(1)
        print '[Control] Listening on port 9091'

    def acceptcsock( this ):
        this.cconn, this.maddr = this.csock.accept()
        print '[Control] Got connection from', this.maddr
        
        while 1:
            data = this.cconn.recv(1024)
            if not data: break
            if data[0:4] == "SEND": this.filename = data[5:]
            print '[Control] Getting ready to receive "%s"' % this.filename
            break

    def transfer( this ):
        print '[Media] Starting media transfer for "%s"' % this.filename

        f = open(this.filename,"wb")
        while 1:
            data = this.mconn.recv(1024)
            if not data: break
            f.write(data)
        f.close()

        print '[Media] Got "%s"' % this.filename
        print '[Media] Closing media transfer for "%s"' % this.filename
    
    def close( this ):
        this.cconn.close()
        this.csock.close()
        this.mconn.close()
        this.msock.close()

    def process( this ):
        while 1:
            this.bindcsock()
            this.acceptcsock()
            this.bindmsock()
            this.acceptmsock()
            this.transfer()
            this.close()

#------------------------------------------------------------------------

s = StreamHandler()
s.start()

Open in new window

0
Comment
Question by:dolamitejenkins
  • 2
3 Comments
 
LVL 17

Expert Comment

by:gelonida
ID: 35716679
sys.argv[1] is the first argument, that you should pass to the command.

So you MUST call the program with a parameter.

Ideally your program should check for this.



You  could insert followign code snippet before line 8:

if len(sys.argv < 2):
    print "ERROR: you must specify the filename as command lien argument"
   sys.exit(1)
0
 
LVL 17

Accepted Solution

by:
gelonida earned 2000 total points
ID: 35716693
Assuming you run the code under WIndows (if not, then please correct me)

Then there's two ways to send the file thisdbfile.sqlite

Either you open a command window and type

FileSender.py thisdbfile.sqlite

or if you like to you can drag and rop the file thisdbfile.sqlite onto the cion of the python script FileSender.py


under linux just type:

python FileSender.py thisdbfile.sqlite



0
 

Author Closing Comment

by:dolamitejenkins
ID: 35725992
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 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