forked from flypythoncom/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyexc.py
More file actions
104 lines (80 loc) · 2.35 KB
/
myexc.py
File metadata and controls
104 lines (80 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 fileAargs(file,mode,agrs):
if args[0] == errno.EACCES and \
'access' in dir(os):
perms = ''
perms = {'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(file,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 myconnnect(sock,host,port):
try:
sock.connect((hosy,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(file,mode ="r"):
try:
fo = open(file,mode)
except IOError,args:
raise FileError,fileArgs(file,mode,args)
return fo
def testfile():
file = mkdtemp()
f = open(file,"w")
f.close()
for eachTest in ((0,"r"),(0100,"r"), \
(0400,"w"),(0500,"w")):
try:
os.chmod(file,eachTest[0])
f=myopen(file,eachTest[1])
except FileError,args:
print "%s: %s" % \
(args.__class__.__name__,args)
else:
print file,"open ok..perm ignored"
f.close()
os.chmod(file,0777)
os.unlink(file)
def testnet():
s = socket.socket(socket.AF_INET.socket.SOCK_STREAM)
for eachHost in ("deli","www"):
try:
myconnect(s,"deli",8080)
except NetworkError,args:
print "%s:%s" %\
(args.__class__.__name__,args)
if __name__ == "__main__":
testfile()
testnet()