1 |
#!/usr/bin/env python |
2 |
|
3 |
# Author: Harald Holzer (harald.holzer@eunet.at) |
4 |
# 5.4.2000: Initial release |
5 |
|
6 |
# 6.5.2000: Harald Holzer (harald.holzer@eunet.at) |
7 |
# added logging support |
8 |
|
9 |
# 13.6.2000: Added patch, for job_new. jobids now returned correctly. |
10 |
# Patch supplied by Victor M. Moreno (vmoreno@geminus.com) |
11 |
|
12 |
import string,sys |
13 |
from socket import * |
14 |
|
15 |
|
16 |
class hfax: |
17 |
s=None |
18 |
sf=None |
19 |
d=None |
20 |
df=None |
21 |
user="" |
22 |
pw="" |
23 |
host="" |
24 |
port=4559 |
25 |
debuglevel=0 |
26 |
logfile="c:\w2hfax.log" |
27 |
|
28 |
def readline(self): |
29 |
data=self.sf.readline() |
30 |
self.loginfo("read: " + data) |
31 |
return data |
32 |
|
33 |
def writeline(self,line): |
34 |
self.loginfo("write: " + line) |
35 |
self.s.send(line) |
36 |
|
37 |
def connect(self,host,user,pw): |
38 |
self.host=host |
39 |
self.user=user |
40 |
self.pw=pw |
41 |
self.s=socket(AF_INET,SOCK_STREAM) |
42 |
try: |
43 |
self.s.connect((host,self.port)) |
44 |
self.sf=self.s.makefile("rw") |
45 |
line=self.readline() |
46 |
self.writeline("USER " + user + "\n") |
47 |
line=self.readline() |
48 |
if line[:3]=="230": |
49 |
rc=0 |
50 |
else: |
51 |
rc=1 |
52 |
except error,detail: |
53 |
self.loginfo("error: " + str(detail)) |
54 |
rc=2 |
55 |
return rc |
56 |
|
57 |
def job_new(self): |
58 |
self.writeline("JOB default\n") |
59 |
self.readline() |
60 |
self.writeline("JNEW\n") |
61 |
data=self.readline() |
62 |
#Victor: 200 NEW job created jobid: 2 groupid: 2. |
63 |
jobid=string.atoi(data[string.find(data,"jobid:")+6:string.find(data,"groupid:")]) |
64 |
return jobid |
65 |
|
66 |
def job_select(self,jobid): |
67 |
self.writeline("JOB " + str(jobid) + "\n") |
68 |
self.readline() |
69 |
|
70 |
def job_kill(self): |
71 |
self.writeline("JKILL\n") |
72 |
self.readline() |
73 |
|
74 |
def job_delete(self): |
75 |
self.writeline("JDELE\n") |
76 |
self.readline() |
77 |
|
78 |
def job_param_getall(self): |
79 |
self.writeline("JPARM\n") |
80 |
self.readline() |
81 |
data="" |
82 |
while 1: |
83 |
line=self.readline() |
84 |
if line[:4]=="217 ": break |
85 |
data=data + line |
86 |
return data |
87 |
|
88 |
def job_param_get(self,pname): |
89 |
self.writeline("JPARM " + pname + "\n") |
90 |
data="" |
91 |
while 1: |
92 |
line = self.readline() |
93 |
data = data + line[4:] |
94 |
if line[3]==" ": break |
95 |
return string.strip(data) |
96 |
|
97 |
def job_param_set(self,pname,pvalue): |
98 |
self.writeline("JPARM " + pname + " " + pvalue +"\n") |
99 |
self.readline() |
100 |
|
101 |
def getfolder(self,folder): |
102 |
self.writeline("PASV\n") |
103 |
data=self.readline() |
104 |
#print data |
105 |
data=data[string.find(data,"(")+1:string.find(data,")")] |
106 |
a1,a2,a3,a4,p1,p2=string.split(data,",") |
107 |
addr=a1 + "." + a2 + "." + a3 + "." + a4 |
108 |
port=string.atoi(p1)*256 + string.atoi(p2) |
109 |
self.d=socket(AF_INET,SOCK_STREAM) |
110 |
self.d.connect((addr,port)) |
111 |
self.df=self.d.makefile() |
112 |
self.writeline("list "+folder+"\n") |
113 |
fdata="" |
114 |
while 1: |
115 |
line=self.df.readline() |
116 |
if not line: break |
117 |
fdata=fdata + line |
118 |
self.df.close() |
119 |
self.d.close() |
120 |
self.readline() |
121 |
self.readline() |
122 |
return fdata |
123 |
|
124 |
def storefile(self,filename): |
125 |
self.writeline("PASV\n") |
126 |
data=self.readline() |
127 |
#print data |
128 |
data=data[string.find(data,"(")+1:string.find(data,")")] |
129 |
a1,a2,a3,a4,p1,p2=string.split(data,",") |
130 |
addr=a1 + "." + a2 + "." + a3 + "." + a4 |
131 |
port=string.atoi(p1)*256 + string.atoi(p2) |
132 |
self.d=socket(AF_INET,SOCK_STREAM) |
133 |
self.d.connect((addr,port)) |
134 |
self.df=self.d.makefile("w") |
135 |
|
136 |
self.writeline("STOT\n") |
137 |
reply=self.readline() |
138 |
sfile=open(filename,"r") |
139 |
while 1: |
140 |
line=sfile.readline() |
141 |
if not line: break |
142 |
self.df.write(line) |
143 |
sfile.close() |
144 |
self.df.close() |
145 |
self.d.close() |
146 |
self.readline() |
147 |
filename=string.split(reply," ")[2] |
148 |
return filename |
149 |
|
150 |
def retrfile(self,filename,tmpfilename): |
151 |
self.writeline("PASV\n") |
152 |
data=self.readline() |
153 |
#print data |
154 |
data=data[string.find(data,"(")+1:string.find(data,")")] |
155 |
a1,a2,a3,a4,p1,p2=string.split(data,",") |
156 |
addr=a1 + "." + a2 + "." + a3 + "." + a4 |
157 |
port=string.atoi(p1) * 256 + string.atoi(p2) |
158 |
self.d=socket(AF_INET,SOCK_STREAM) |
159 |
self.d.connect((addr,port)) |
160 |
self.df=self.d.makefile("rb") |
161 |
|
162 |
self.writeline("RETR " + filename + "\n") |
163 |
reply=self.readline() |
164 |
sfile=open(tmpfilename,"wb") |
165 |
line=self.df.read() |
166 |
#while 1: |
167 |
# line=self.df.readline() |
168 |
# if not line: break |
169 |
# sfile.write(line) |
170 |
sfile.write(line) |
171 |
sfile.close() |
172 |
self.df.close() |
173 |
self.d.close() |
174 |
self.readline() |
175 |
return reply |
176 |
|
177 |
def deletefile(self,name): |
178 |
self.writeline("DELE " + name + "\n") |
179 |
self.readline() |
180 |
|
181 |
def jobparam(self,param): |
182 |
self.writeline("JPARM " + param+"\n") |
183 |
self.readline() |
184 |
|
185 |
def submitjob(self): |
186 |
self.writeline("JSUBM\n") |
187 |
self.readline() |
188 |
|
189 |
def docmd(self,cmd): |
190 |
self.writeline(cmd+"\n") |
191 |
self.readline() |
192 |
|
193 |
def close(self): |
194 |
self.sf.close() |
195 |
self.s.close() |
196 |
|
197 |
def setdebuginfo(self,dbglevel,logfile): |
198 |
self.debuglevel=dbglevel |
199 |
self.logfile=logfile |
200 |
|
201 |
def loginfo(self,info): |
202 |
if self.debuglevel>0: |
203 |
f=open(self.logfile,"a") |
204 |
f.write(info+"\r\n") |
205 |
f.close |
206 |
|
207 |
if __name__=="__main__": |
208 |
host="127.0.0.1" |
209 |
if len(sys.argv)>1: |
210 |
listdir=sys.argv[1] |
211 |
else: |
212 |
listdir="/" |
213 |
test=hfax() |
214 |
test.connect(host,"root") |
215 |
print test.getfolder(listdir) |
216 |
test.close() |
217 |
|