import socket class SDCard: def __init__(self,home_dir=''): self.home_dir=home_dir # ip , self.ip=socket.gethostbyname(socket.gethostname()) # ip self.card_ip=None if __name__=='__main__': # HOME_DIR=os.path.expanduser('~') if not os.path.exists(HOME_DIR+'/'+'ShootAndView'): os.mkdir(HOME_DIR+'/'+'ShootAndView') HOME_DIR=HOME_DIR+'/ShootAndView/' sd=SDCard(home_dir=HOME_DIR)
Transcend WiFiSD - interface=mlan0 ip=192.168.0.16 netmask=255.255.255.0 router=192.168.0.1 mode=client essid=WiFiSDCard apmac=CE:5D:4E:5B:70:48
import os import socket import thread import time class SDCard: def __init__(self,home_dir=''): self.home_dir=home_dir self.ip=socket.gethostbyname(socket.gethostname()) self.card_ip=None def find_card(self,callback=None): """ """ thread.start_new_thread(self.find_card_thread,(callback,)) def find_card_thread(self,callback=None): while not self.card_ip: """ UDP """ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(5) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) """ """ try:s.bind((self.ip, 58255)) except socket.error: s.close() time.sleep(1) continue """ 55777""" s.sendto('', ('<broadcast>', 55777)) try: resp=s.recv(400) s.close() try: """ """ self.card_ip=resp.split('ip=')[1].split('\n')[0] except IndexError: """ """ if callback:callback(None) """ ip""" if callback:callback(self.card_ip) except socket.timeout: callback(self.card_ip) finally: time.sleep(2) def monitor(ip): if not ip:return print 'Find card on ip:',ip if __name__=='__main__': HOME_DIR=os.path.expanduser('~') if not os.path.exists(HOME_DIR+'/'+'ShootAndView'): os.mkdir(HOME_DIR+'/'+'ShootAndView') HOME_DIR=HOME_DIR+'/ShootAndView/' if options.dir:HOME_DIR=options.dir sd=SDCard(home_dir=HOME_DIR) # "", # GUI sd.find_card(callback=monitor) # , # while 1: time.sleep(1)
>/mnt/DCIM/101CANON/IMG_1754.JPG
def listener_thread(self,callback): sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.connect((self.card_ip, 5566)) while self.listen_flag: message=sock.recv(1024) # ( ) new_files=message.split('\00') for x in new_files: if x: # self.all_files.append(x[1:]) # x[1:] - ">", # self.download_list.put(self.all_files[-1]) if callback:callback(self.all_files[-1])
def download_thread(self,download_callback,download_complete): while self.listen_flag: # if not self.download_list.empty(): # fl=self.download_list.get(block=0) # urllib.urlretrieve('http://%s/cgi-bin/wifi_download?fn=%s'%(self.card_ip,fl),self.home_dir+fl.split('/')[-1],download_callback if download_callback else None) if download_complete:download_complete(self.download_now) time.sleep(0.1)
#coding:utf-8 """ Copyright (C) 2010 Igor zalomskij <igor.kaist@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ import os import socket import thread import time import ping import Queue import urllib import sys class SDCard: def __init__(self,home_dir=''): self.home_dir=home_dir # ip self.ip=socket.gethostbyname(socket.gethostname()) self.card_ip=None # ip self.all_files=[] # self.download_list=Queue.Queue() # self.in_queue=[] # , GUI def find_card(self,callback=None): # thread.start_new_thread(self.find_card_thread,(callback,)) def find_card_thread(self,callback=None): """ """ while not self.card_ip: # UDP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(5) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) try:s.bind((self.ip, 58255)) # except socket.error: s.close() time.sleep(1) continue # 55777 s.sendto('', ('<broadcast>', 55777)) try: resp=s.recv(400) s.close() try: # self.card_ip=resp.split('ip=')[1].split('\n')[0] except IndexError: # if callback:callback(None) if callback:callback(self.card_ip) except socket.timeout: callback(None) finally: time.sleep(2) def start_listen(self,callback=None,download_callback=None,download_complete=None): """ . """ self.listen_flag=True # , thread.start_new_thread(self.listener_thread,(callback,)) # , . thread.start_new_thread(self.ping_card,()) # thread.start_new_thread(self.download_thread,(download_callback,download_complete)) def ping_card(self): # 20 . while self.listen_flag: try: resp=ping.do_one(self.card_ip) except socket.error: # , pass time.sleep(20) def listener_thread(self,callback): # sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 5566 sock.connect((self.card_ip, 5566)) while self.listen_flag: message=sock.recv(1024) new_files=message.split('\00') # ( ) for x in new_files: if x: # ;) self.all_files.append(x[1:]) # x[1:] ">", self.download_list.put(self.all_files[-1]) # self.in_queue.append(self.all_files[-1]) # , GUI if callback:callback(self.all_files[-1]) def download_thread(self,download_callback,download_complete): # while self.listen_flag: if not self.download_list.empty(): # fl=self.download_list.get(block=0) self.download_now=fl # , GUI # urllib.urlretrieve('http://%s/cgi-bin/wifi_download?fn=%s'%(self.card_ip,fl),self.home_dir+fl.split('/')[-1],download_callback if download_callback else None) if download_complete:download_complete(self.download_now) time.sleep(0.1) def find_callback(ip): if not ip:return print 'Find card on ip:',ip # IP , sd.start_listen(download_complete=download_complete) def download_complete(fname): print 'New image: %s'%(HOME_DIR+fname.split('/')[-1]) if __name__=='__main__': """ , . , ip """ from optparse import OptionParser parser = OptionParser() parser.add_option("-d", "--dir", dest="dir",default=None,help="directory for storing images") parser.add_option("-i", "--ip", dest="ip",default=None,help="ip address of the computer (default %s)"%(socket.gethostbyname(socket.gethostname()))) (options, args) = parser.parse_args() # . HOME_DIR=os.path.expanduser('~') if not os.path.exists(HOME_DIR+'/'+'ShootAndView'): os.mkdir(HOME_DIR+'/'+'ShootAndView') HOME_DIR=HOME_DIR+'/ShootAndView/' if options.dir:HOME_DIR=options.dir sd=SDCard(home_dir=HOME_DIR) if options.ip:sd.ip=options.ip print 'Finding sd card...' # sd.find_card(callback=find_callback) while 1: time.sleep(1)
from Tkinter import *
sudo apt-get install python-tk python-imagetk python-imaging libimage-exiftool-perl
sudo python setup.py install
)Source: https://habr.com/ru/post/216039/
All Articles