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


i writing python http client. when have code below error message 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]) 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 program command line typing

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

could show me what's wrong code? in advance.

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

import argparse ap  def argparse():     parser=ap.argumentparser(description='script something')     parser.add_argument("host", help="web address")     parser.add_argument("port", help="port connect on")     parser.add_argument("filename",help="page use")     return parser.parse_args() 

then in main function call:

args=argparse() 

then can use args.host, args.port , args.filename in code , argparse handle missing arguments , types etc. can use optional arguments adding -- in front of definitions in add_argument() call.

your code using argparse:

from socket import * import argparse ap  def argparse():     parser=ap.argumentparser(description='script something')     parser.add_argument("host", help="web address")     parser.add_argument("port", type=int, help="port connect on")     parser.add_argument("filename",help="page 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]) 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 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -