1
回答
Traceback (most recent call last):
终于搞明白,存储TCO原来是这样算的>>>   
import sys
import socket
import argparse

def main():
    # setup argument parsing
    parser = argparse.ArgumentParser(description='Socket Error Examples')
    parser.add_argument('--host', action="store", dest="host", required=False)
    parser.add_argument('--port', action="store", dest="port", type=int, required=False)
    parser.add_argument('--file', action="store", dest="file", required=False)
    given_args = parser.parse_args()
    host = given_args.host
    port = given_args.port
    filename = given_args.file

    #First try-except block -- create socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, e:
        print "Error creating socket: %s" % e
        sys.exit(1)

    #second try-except block -- connect to given host/post
    try:
        s.connect((host, port))
    except socket.gaierror, e:
        print "Address-related error connecting to sever: %s" % e
        sys.exit(1)
    except socket.error, e:
        print "Connection error: %s" % e
        sys.exit(1)

    #Third try-except block -- sending data
    try:
        s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)
    except socket.error, e:
        print "Error sending data: %s" % e
        sys.exit(1)
    
    while 1:
        #Fourth try-except block -- waiting to receive data from remote host
        try:
            buf = s.recv(2048)
        except socket.error, e:
            print "Error receiving data : %s" % e
            sys.exit(1)
        if  not len(buf):
            break
        #write the received data
        sys.stdout.write(buf)
if __name__ == '__main__':
    main()



各位大神好,我刚开始学Python   这些都是照着书本上敲得,但是一直报错,搜索了一下也没明白为什么。希望大神能帮忙解答

举报
菜鸟运维
发帖于2年前 1回/5K+阅
顶部