python使用socket远程连接错误处理方法
本文实例讲述了python使用socket远程连接错误处理方法。分享给大家供大家参考。具体如下:
importsocket,sys host=sys.argv[1] textport=sys.argv[2] filename=sys.argv[3] try: s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) exceptsocket.error,e: print"Strangeerrorcreatingsocket:%s"%e sys.exit(1) try: port=int(textport) exceptValueError: print"Couldn'tfindyourport:%s"%e sys.exit(1) try: s.connect((host,port)) exceptsocket.gaierror,e: print"Address-relatederrorconnectingtoserver:%s"%e sys.exit(1) exceptsocket.error,e: print"Connectionerror:%s"%e sys.exit(1) try: s.sendall("GET%sHTTP/1.0\r\n\r\n"%filename) exceptsocket.error,e: print"Errorsendingdata:%s"%e sys.exit(1) while1: try: buf=s.recv(2048) exceptsocket.error,e: print"Errorreceivingdata:%s"%e sys.exit(1) ifnotlen(buf): break sys.stdout.write(buf)
希望本文所述对大家的Python程序设计有所帮助。