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

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


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
dolamitejenkins
Asked:
dolamitejenkins
  • 2
1 Solution
 
gelonidaCommented:
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
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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