- 论坛徽章:
- 11
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ppdcreate
import time
from socket import *
import os
import sys
import threading
import re
import gobject
PORT=12321
ip_begin=1
ip_end=254
ip_list=[]
mutex = threading.Lock()
gobject.threads_init()
class MyWindow(threading.Thread):
def __init__(self, title, width, height):
threading.Thread.__init__(self)
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title(title)
self.window.set_size_request(width, height)
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.set_border_width(
self.window.connect('destroy', self.destroy_mainloop)
self.window.set_resizable(False)
self.table = gtk.Table(3, 3, False)
self.table.set_col_spacings(3)
self.entry=gtk.Entry()
self.table.attach(self.entry, 1, 2, 0, 1, gtk.FILL , gtk.FILL , 0, 0);
self.store = self.create_model()
self.treeView = gtk.TreeView(self.store)
#self.create_columns()
#self.table.attach(self.treeView, 1, 2, 0, 1, gtk.FILL | gtk.EXPAND,gtk.FILL | gtk.EXPAND, 0, 0);
self.cb = gtk.combo_box_new_text()
self.cb.connect("changed", self.on_changed)
self.cb.set_size_request(100, 30)
self.cb.append_text('手动输入')
self.cb.append_text('自动搜索')
self.cb.set_active(0)
halign_cb = gtk.Alignment(0, 0, 0, 0)
halign_cb.add(self.cb)
self.table.attach(halign_cb, 0, 1, 0, 1, gtk.FILL,gtk.FILL, 0, 0);
self.ok = gtk.Button("OK"
self.ok.set_size_request(70, 30)
halign_ok = gtk.Alignment(1, 0, 0, 0)
halign_ok.add(self.ok)
self.table.attach(halign_ok, 1, 2, 1, 2, gtk.FILL,gtk.FILL, 0, 0);
self.ok.connect("clicked", self.on_ok_clicked)
self.window.statusbar = gtk.Statusbar()
self.table.attach(self.window.statusbar, 0, 3, 2, 3, gtk.FILL| gtk.EXPAND,gtk.FILL| gtk.EXPAND, 0, 0);
self.window.statusbar.push(1,"准备"
self.window.add(self.table)
self.window.show_all()
def create_columns(self):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Ip", rendererText, text=0)
column.set_sort_column_id(0)
self.treeView.append_column(column)
def destroy_mainloop(self,widget):
global ip_begin
ip_begin=0
gtk.main_quit()
def create_model(self):
store = gtk.ListStore(str)
return store
def on_ok_clicked(self,widget):
global ip_list
if self.cb.get_active_text() == "手动输入":
if not re.findall(r"(?<![0-9.])((2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2})\.){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9]{1,2})(?![0-9.])", self.entry.get_text()):
self.window.statusbar.push(1,"IP输入有误!"
return
self.cb.set_sensitive(False)
widget.set_sensitive(False)
ip_list=[]
ip_list.append(self.entry.get_text())
elif self.cb.get_active_text() == "自动搜索":
self.cb.set_sensitive(False)
widget.set_sensitive(False)
self.window.statusbar.push(1,"服务端IP检测中....."
threads = []
for x in xrange(0 , 20):
t_scan = Scan(PORT , 1)
threads.append(t_scan)
for t in threads:
t.start()
def on_changed(self, widget):
if widget.get_active_text() == "手动输入":
self.treeView.hide()
self.entry.show()
else:
self.entry.hide()
self.table.attach(self.treeView, 1, 2, 0, 1, gtk.FILL | gtk.EXPAND,gtk.FILL | gtk.EXPAND, 0, 0);
self.treeView.show()
def run(self):
gtk.main()
class Scan(threading.Thread):
def __init__(self , port , timeout):
threading.Thread.__init__(self)
self.port = port
self.timeout = timeout
self.local_ip=self.__get_ip_address()
def run(self):
global mutex ,ip_begin,ip_end
while 1:
mutex.acquire()
ip_begin += 1
if ip_begin > ip_end:
mutex.release()
break
mutex.release()
ip=re.sub("\d+$",str(ip_begin),self.local_ip)
self.ping(ip , self.port , self.timeout)
def __get_ip_address(self,ifname='eth0'):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
except:
ips = os.popen("LANG=C ifconfig | grep \"inet addr\" | grep -v \"127.0.0.1\" | awk -F \":\" '{print $2}' | awk '{print $1}'" .readlines()
if len(ips) > 0:
return ips[0]
return ''
def ping(self,ip ,port ,timeout=1):
try:
global ip_list
cs=socket(AF_INET,SOCK_STREAM)
cs.settimeout(float(timeout))
address=(str(ip),int(port))
status = cs.connect_ex((address))
if status == 0 :
ip_list.append(ip)
except Exception ,e:
print "IPScan:%s" %e
cs.close()
return 0
class Communication:
def __init__(self, port,ip):
self.__bufSize=8192
self.remote_ip=ip
self.port=port
self.__unixServSock = socket(AF_UNIX, SOCK_STREAM)
if os.path.exists("/tmp/fdosprinter.domain" :
os.unlink("/tmp/fdosprinter.domain"
self.__unixServSock.bind("/tmp/fdosprinter.domain"
self.__unixServSock.listen(10)
self.PPDFile=ppdcreate.CreatePPDFile("/etc/cups/ppd/"
def queryPrinter(self):
try:
self.__tcpCliSock=socket(AF_INET, SOCK_STREAM)
self.__tcpCliSock.connect((self.remote_ip,self.port))
except Exception as e:
global ip_list,ip_begin,ip_end
threads = []
for x in xrange(0 , 20):
t_scan = Scan(PORT , 1)
threads.append(t_scan)
for t in threads:
t.start()
while 1:
if ip_begin < ip_end and ip_list:
self.remote_ip=ip_list[0]
with open("/etc/cups/printer_server_ip_conf","w+" as f:
f.write(ip_list[0])
print "IP 检测完成"
try:
self.__tcpCliSock=socket(AF_INET, SOCK_STREAM)
self.__tcpCliSock.connect((self.remote_ip,self.port))
break
except Exception as e:
print "IP 检测不到!!!!!!"
return
elif ip_begin > ip_end and not ip_list:
print "IP 检测不到!!!!!!"
return
else:
pass
try:
data="0"
self.__tcpCliSock.send(data)
data=self.__tcpCliSock.recv(self.__bufSize)
if not data:
return "windows端请求不到数据,不存在打印机?"
print data
self.PPDFile.factory(data)
except Exception as e:
#print "queryPrinter:",e
return ""
def update_label(self, message,statusbar):
statusbar.push(1, message)
return False
def waitUnix(self):
global ip_begin,ip_end,ip_list
#data=self.queryPrinter()
while 1:
#if not self.__unixServSock:
self.__unixServSock = socket(AF_UNIX, SOCK_STREAM)
if os.path.exists("/tmp/fdosprinter.domain"):
os.unlink("/tmp/fdosprinter.domain")
self.__unixServSock.bind("/tmp/fdosprinter.domain")
self.__unixServSock.listen(10)
connection, address = self.__unixServSock.accept()
message=connection.recv(1024)
if re.findall("SERVER IP LOST",message):
import gtk
window = MyWindow('方德远程打印机', 280, 100) # 创建窗口对象
window.start()
while 1:
if ip_begin == 0:
break
if ip_begin < ip_end and ip_list:
gobject.idle_add(self.update_label, "获取到IP", window.window.statusbar)
if os.path.exists("/etc/cups/printer_server_ip_conf"):
os.unlink("/etc/cups/printer_server_ip_conf")
with open("/etc/cups/printer_server_ip_conf","w+") as f:
f.write(ip_list[0])
self.remote_ip=ip_list[0]
window.store=ip_list[0]
gobject.idle_add(self.update_label, "安装虚拟打印机.....", window.window.statusbar)
#self.queryPrinter()
gobject.idle_add(self.update_label, "安装完成,打印机配置可能已经改变,请重新打印", window.window.statusbar)
#self.PPDFile.deleteCupsConf("FDOS_Virtual_Printer")
break
elif ip_begin > ip_end and not ip_list:
gobject.idle_add(self.update_label, "未找到局域网内的windows打印机,请检查IP", window.window.statusbar)
break
else:
pass
ip_begin=1
ip_list=[]
connection.close()
self.__unixServSock.close()
def close(self):
self.__tcpCliSock.close()
self.__unixServSock.close()
if __name__ == '__main__':
window = MyWindow('印机', 280, 100) # 创建窗口对象
window.start()
|
|