| 199 | def supportsSNMP(self, hostname, community) : |
| 200 | """Returns 1 if the printer accepts SNMP queries, else 0.""" |
| 201 | # TODO : do some real testing here ! |
| 202 | return 0 |
| 203 | snmpsupport = 0 |
| 204 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 205 | try : |
| 206 | s.connect((hostname, socket.getservbyname("snmp", "udp"))) |
| 207 | for i in range(2) : |
| 208 | s.send("") |
| 209 | except : |
| 210 | pass |
| 211 | else : |
| 212 | snmpsupport = 1 |
| 213 | s.close() |
| 214 | return snmpsupport |
| 215 | |
| 216 | def supportsPJL(self, hostname, port) : |
| 217 | """Returns 1 if the printer accepts PJL queries over TCP, else 0.""" |
| 218 | def alarmHandler(signum, frame) : |
| 219 | raise "Timeout !" |
| 220 | |
| 221 | pjlsupport = 0 |
| 222 | signal.signal(signal.SIGALRM, alarmHandler) |
| 223 | signal.alarm(2) # wait at most 2 seconds |
| 224 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 225 | try : |
| 226 | s.connect((hostname, port)) |
| 227 | s.send("\033%-12345X@PJL INFO STATUS\r\n\033%-12345X") |
| 228 | answer = s.recv(1024) |
| 229 | if not answer.startswith("@PJL") : |
| 230 | raise "No PJL !" |
| 231 | except : |
| 232 | pass |
| 233 | else : |
| 234 | pjlsupport = 1 |
| 235 | s.close() |
| 236 | signal.alarm(0) |
| 237 | signal.signal(signal.SIGALRM, signal.SIG_IGN) |
| 238 | return pjlsupport |
| 239 | |
243 | | # check udp/161 (SNMP) |
244 | | # TODO : this doesn't work as expected if the host |
245 | | # TODO : is down, I must find another solution. |
246 | | trysnmp = 0 |
247 | | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
248 | | try : |
249 | | s.connect((hostname, socket.getservbyname("snmp", "udp"))) |
250 | | for i in range(2) : |
251 | | s.send("") |
252 | | except : |
253 | | pass |
254 | | else : |
255 | | trysnmp = 1 |
256 | | s.close() |
257 | | |
258 | | # check tcp/9100 |
259 | | trypjl = 0 |
260 | | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
261 | | try : |
262 | | s.connect((hostname, port)) |
263 | | except : |
264 | | pass |
265 | | else : |
266 | | trypjl = 1 |
267 | | s.close() |
268 | | |
269 | | if trysnmp : |
| 285 | if self.supportsPJL(hostname, 9100) : |
| 286 | accounter = "hardware(pjl)" |
| 287 | elif self.supportsPJL(hostname, 9101) : |
| 288 | accounter = "hardware(pjl:9101)" |
| 289 | elif self.supportsPJL(hostname, port) : |
| 290 | accounter = "hardware(pjl:%s)" % port |
| 291 | elif self.supportsSNMP(hostname, "public") : |