简体   繁体   中英

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

I am writing a Python HTTP client. When I have the code below I get an error message from the terminal "list index out of range".

from socket import *
import sys

server_host = sys.argv[1]
server_port = sys.argv[2]
filename = sys.argv[3]

host_port = "%s:%s" %(server_host, server_port)
try:
    clientSocket = socket(AF_INET,SOCK_STREAM)
    clientSocket.connect((server_host,int(server_port)))
    header = {
    "first_header" : "GET /%s HTTP/1.1" %(filename),
    "Host": host_port,
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US",
    }
    httpHeader = "\r\n".join("%s:%s" %(item,header[item]) for item in     header)
    print httpHeader
    clientSocket.send("%s\r\n\r\n" %(httpHeader))

except IOError:

    sys.exit(1)
final = ""
responseMessage = clientSocket.recv(1024)
while responseMessage:
    final += responseMessage
    responseMssage = clientSocket.recv(1024)

clientSocket.close()
print "final:",final

I run the program from the command line by typing

python ./client.py http://127.0.0.1 7000 HelloWorld.html

Could anybody show me what's wrong with my code? Thanks in advance.

Why not use argparse instead of handling sys.argv manually? For example:

import argparse as ap

def argParse():
    parser=ap.ArgumentParser(description='Script to do something')
    parser.add_argument("host", help="web address")
    parser.add_argument("port", help="port to connect on")
    parser.add_argument("filename",help="page to use")
    return parser.parse_args()

Then in the main function call:

args=argParse()

Then you can use args.host , args.port and args.filename in your code and argparse will handle missing arguments and they types etc. You can use optional arguments by adding -- in front of their definitions in the add_argument() call.

Your code using argparse:

from socket import *
import argparse as ap

def argParse():
    parser=ap.ArgumentParser(description='Script to do something')
    parser.add_argument("host", help="web address")
    parser.add_argument("port", type=int, help="port to connect on")
    parser.add_argument("filename",help="page to use")
    return parser.parse_args()

args=argParse()

host_port = "%s:%d" % (args.host, args.port)
try:
    clientSocket = socket(AF_INET,SOCK_STREAM)
    clientSocket.connect((args.host,args.port))
    header = {
    "first_header" : "GET /%s HTTP/1.1" % (args.filename),
    "Host": str(args.port),
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US",
    }
    httpHeader = "\r\n".join("%s:%s" %(item,header[item]) for item in     header)
    print httpHeader
    clientSocket.send("%s\r\n\r\n" %(httpHeader))

except IOError:

    sys.exit(1)
final = ""
responseMessage = clientSocket.recv(1024)
while responseMessage:
    final += responseMessage
    responseMssage = clientSocket.recv(1024)

clientSocket.close()
print "final:",final

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM