|
| 1 | + |
| 2 | +import os,socket,errno,types,tempfile |
| 3 | + |
| 4 | +class NetworkError(IOError): |
| 5 | + pass |
| 6 | + |
| 7 | +class FileError(IOError): |
| 8 | + pass |
| 9 | + |
| 10 | +def updArgs(args,newarg=None): |
| 11 | + if isinstance(args,IOError): |
| 12 | + myargs =[] |
| 13 | + myargs.extend([arg for arg in args]) |
| 14 | + else: |
| 15 | + myargs = list(args) |
| 16 | + |
| 17 | + if newarg: |
| 18 | + myargs.append(newarg) |
| 19 | + |
| 20 | + return tuple(myargs) |
| 21 | + |
| 22 | +def fileAargs(file,mode,agrs): |
| 23 | + if args[0] == errno.EACCES and \ |
| 24 | + 'access' in dir(os): |
| 25 | + perms = '' |
| 26 | + perms = {'r':os.R_OK,'w':os.W_OK,'x':os.X_OK} |
| 27 | + pkeys = permd.keys() |
| 28 | + pkeys.sort() |
| 29 | + pkeys.reverse() |
| 30 | + |
| 31 | + for eachPerm in 'rwx': |
| 32 | + if os.access(file,permd[eachPerm]): |
| 33 | + perms += eachPerm |
| 34 | + else: |
| 35 | + perms += '-' |
| 36 | + |
| 37 | + if isinstance(args,IOError): |
| 38 | + myargs = [] |
| 39 | + myargs.extend([arg for arg in args]) |
| 40 | + else: |
| 41 | + myargs = list(args) |
| 42 | + |
| 43 | + myargs[1] = "'%s' %s (perms:'%s')" % (mode,myargs[1],perms) |
| 44 | + |
| 45 | + myargs.append(args.filename) |
| 46 | + |
| 47 | + else: |
| 48 | + myargs = args |
| 49 | + return tuple(myargs) |
| 50 | + |
| 51 | +def myconnnect(sock,host,port): |
| 52 | + try: |
| 53 | + sock.connect((hosy,port)) |
| 54 | + except socket.error,args: |
| 55 | + myargs = updArgs(args) |
| 56 | + if len(myargs) == 1: |
| 57 | + myargs = (errno.ENXIO,myargs[0]) |
| 58 | + |
| 59 | + raise NetworkError,\ |
| 60 | + updArgs(myargs,host +":" + str(port)) |
| 61 | +def myopen(file,mode ="r"): |
| 62 | + try: |
| 63 | + fo = open(file,mode) |
| 64 | + except IOError,args: |
| 65 | + raise FileError,fileArgs(file,mode,args) |
| 66 | + return fo |
| 67 | + |
| 68 | +def testfile(): |
| 69 | + file = mkdtemp() |
| 70 | + f = open(file,"w") |
| 71 | + f.close() |
| 72 | + |
| 73 | + for eachTest in ((0,"r"),(0100,"r"), \ |
| 74 | + (0400,"w"),(0500,"w")): |
| 75 | + try: |
| 76 | + os.chmod(file,eachTest[0]) |
| 77 | + f=myopen(file,eachTest[1]) |
| 78 | + |
| 79 | + except FileError,args: |
| 80 | + print "%s: %s" % \ |
| 81 | + (args.__class__.__name__,args) |
| 82 | + else: |
| 83 | + print file,"open ok..perm ignored" |
| 84 | + f.close() |
| 85 | + os.chmod(file,0777) |
| 86 | + os.unlink(file) |
| 87 | +def testnet(): |
| 88 | + s = socket.socket(socket.AF_INET.socket.SOCK_STREAM) |
| 89 | + for eachHost in ("deli","www"): |
| 90 | + try: |
| 91 | + myconnect(s,"deli",8080) |
| 92 | + except NetworkError,args: |
| 93 | + print "%s:%s" %\ |
| 94 | + (args.__class__.__name__,args) |
| 95 | +if __name__ == "__main__": |
| 96 | + testfile() |
| 97 | + testnet() |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments