|
| 1 | + |
| 2 | +importos,socket,errno,types,tempfile |
| 3 | + |
| 4 | +classNetworkError(IOError): |
| 5 | +pass |
| 6 | + |
| 7 | +classFileError(IOError): |
| 8 | +pass |
| 9 | + |
| 10 | +defupdArgs(args,newarg=None): |
| 11 | +ifisinstance(args,IOError): |
| 12 | +myargs=[] |
| 13 | +myargs.extend([argforarginargs]) |
| 14 | +else: |
| 15 | +myargs=list(args) |
| 16 | + |
| 17 | +ifnewarg: |
| 18 | +myargs.append(newarg) |
| 19 | + |
| 20 | +returntuple(myargs) |
| 21 | + |
| 22 | +deffileAargs(file,mode,agrs): |
| 23 | +ifargs[0]==errno.EACCESand \ |
| 24 | +'access'indir(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 | +foreachPermin'rwx': |
| 32 | +ifos.access(file,permd[eachPerm]): |
| 33 | +perms+=eachPerm |
| 34 | +else: |
| 35 | +perms+='-' |
| 36 | + |
| 37 | +ifisinstance(args,IOError): |
| 38 | +myargs= [] |
| 39 | +myargs.extend([argforarginargs]) |
| 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 | +returntuple(myargs) |
| 50 | + |
| 51 | +defmyconnnect(sock,host,port): |
| 52 | +try: |
| 53 | +sock.connect((hosy,port)) |
| 54 | +exceptsocket.error,args: |
| 55 | +myargs=updArgs(args) |
| 56 | +iflen(myargs)==1: |
| 57 | +myargs= (errno.ENXIO,myargs[0]) |
| 58 | + |
| 59 | +raiseNetworkError,\ |
| 60 | +updArgs(myargs,host+":"+str(port)) |
| 61 | +defmyopen(file,mode="r"): |
| 62 | +try: |
| 63 | +fo=open(file,mode) |
| 64 | +exceptIOError,args: |
| 65 | +raiseFileError,fileArgs(file,mode,args) |
| 66 | +returnfo |
| 67 | + |
| 68 | +deftestfile(): |
| 69 | +file=mkdtemp() |
| 70 | +f=open(file,"w") |
| 71 | +f.close() |
| 72 | + |
| 73 | +foreachTestin ((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 | +exceptFileError,args: |
| 80 | +print"%s: %s"% \ |
| 81 | + (args.__class__.__name__,args) |
| 82 | +else: |
| 83 | +printfile,"open ok..perm ignored" |
| 84 | +f.close() |
| 85 | +os.chmod(file,0777) |
| 86 | +os.unlink(file) |
| 87 | +deftestnet(): |
| 88 | +s=socket.socket(socket.AF_INET.socket.SOCK_STREAM) |
| 89 | +foreachHostin ("deli","www"): |
| 90 | +try: |
| 91 | +myconnect(s,"deli",8080) |
| 92 | +exceptNetworkError,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 | + |