#! /usr/bin/env python

import os, socket, errno, types, tempfile

class NetworkError(IOError):
    pass

class FileError(IOError):
    pass

def updArgs(args, newarg=None):
    if isinstance(args, IOError):
        myargs = []
        myargs.extend([arg for arg in args])
    else:
        myargs = list(args)

    if newarg:
        myargs.append(newarg)

    return tuple(myargs)

def fileArgs(fn, mode, args):
    if args[0] == errno.EACCES and 'accesss' in dir(os):
        perms = ''
        permd = {'r': os.R_OK, 'w': os.W_OK, 'x': os.X_OK}
        pkeys = permd.keys()
        pkeys.sort()
        pkeys.reverse()

        for eachPerm in 'rwx':
            if os.access(fn, permd[eachPerm]):
                perms += eachPerm
            else:
                perms += '-'

        if isinstance(args, IOError):
            myargs = []
            myargs.extend([arg for arg in args])
        else:
            myargs = list(args)

        myargs[1] = "'%s' %s (perms: '%s')" % \
                    (mode, myargs[1], perms)

        myargs.append(args.filename)

    else:
        myargs = args

    return tuple(myargs)

def myconnect(sock, host, port):
    try:
        sock.connect((host, port))
    except socket.error, args:
        myargs = updArgs(args)
        if len(myargs) == 1:
            myargs = (errno.ENXIO, myargs[0])
        raise NetworkError, \
             updArgs(myargs, host +':' + str(port))

def myopen(fn, mode='r'):
    try:
        fo = open(fn, mode)
    except IOError, args:
        raise FileError, fileArgs(fn, mode, args)

    return fo

def testfile():
    fn = tempfile.mktemp()
    f = open(fn, 'w')
    f.close()

    for eachTest in ((0, 'r'), (0100, 'r'), (0400, 'w'), (0500, 'w')):
        try:
            os.chmod(fn, eachTest[0])
            f = myopen(fn, eachTest[1])
        except FileError, args:
            print "%s: %s" % \
                (args.__class__.__name__, args)
        else:
            print fn, "opened ok... perm ignored"
            f.close()

    os.chmod(fn, 0777)
    os.unlink(fn)

def testnet():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for eachHost in ('deli', 'www'):
        try:
            myconnect(s, eachHost, 8080)
        except NetworkError, args:
            print "%s: %s" % \
                  (args.__class__.__name__, args)

if __name__ == '__main__':
    testfile()
    testnet()

c:\docume~1\beyond~1\locals~1\temp\tmpaphqao opened ok... perm ignored
c:\docume~1\beyond~1\locals~1\temp\tmpaphqao opened ok... perm ignored
FileError: [Errno 13] Permission denied
FileError: [Errno 13] Permission denied
NetworkError: [Errno 11001] getaddrinfo failed: 'deli:8080'
NetworkError: [Errno 11001] getaddrinfo failed: 'www:8080'

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐