先来说下原理:用c程序调用ioctl驱动来调用键盘上的显示灯。使其按照我们的程序来实现亮灭。再来说下我要做这个功能的初衷:现在有一种lifi技术,也就是各国都在研空的利用可见光上网的技术。这种技术的优点和缺点在网上一搜一大把,我就不在这里缀述了。我要做在是:实现这种可见光并将其接收,在实现通讯的基础上,解决数据上行的难题。好了,不多说,看代码。
先来个例子,简单画房子(带烟囱) LOGO语言是为教育儿童编程开发的,所以语法很简单。它基于LISP,还附加有一个海龟绘图系统(Turtle Graphic)。我们通过编写的程序,可以控制屏幕上的小海龟的移动,并在画面上留下行动的轨迹。你可以用LOGO语言画出一幅不错的画来。于是在Ubuntu下搜索,找到KTurtle,是LOGO的一个变体。它的语法是面向过程的,类似于C,支持数学和逻辑运算,可以实现循环 (for)或者选择 (if)。常见的命令包括forward, backward, turnleft, turnright,用于控制小海龟的前进后退和转向。另外也可以用ask和print来输入输出。变量用$a的形式表示。在绘图过程中,小海龟不断移动,同时左侧也会用黄色标明运行到哪一行了,非常有趣。一句话来形容,麻雀虽小,五脏俱齐。
迷信是什么?科学又是什么?这个问题很少人有过仔细研究…
#=============================================================================
#=============================================================================
啥都不说了,看代码。小孩子自从上了幼儿园以后,老师就将好多孩子在幼儿园的照片发到了网上,由于数量很多,一张一张下载起来太费劲了,故作此程序,希望能给自己带来方便的同时也给大家带来方便。 好,看代码:
格里高利历法改革(Gregorian Reformation)被认为发生于 1752 年 9 月 3 日. 在此之前, 多数国家已经认可这项改革( 尽管有一些直到 20 世纪初才认可它).
我用的是Ubuntu的Server版本,安装是很容易的,非常之简单,不过安装之后的操作系统几乎是一个空白的操作系统,几乎什么都没有,所以都得安装好。
现在,很多人都知道,Python 里有个 SimpleHTTPServer,可以拿来方便地共享文件。比如,你要发送某个文件给局域网里的同学,你只要 cd 到所在路径,然后执行这么一行: python -m SimpleHTTPServer 人家就可以通过 http://你的IP:8000 来访问你要共享的文件了。像我早已把这个命令做了 alias。但是,某一天,你需要从同学哪里复制一个文件到本机,然后你就会跟你同学说,XX,共享下某目录。当你以为可以用 HTTP 来访问他的 8000 端口的时候,他却告诉你,不好意思,我是 Windows 啦~~当然你可以选择在他 Windows 里装个 Python,也可以选择使用 Samba、FTP 等其他方式,但是有没有和之前一样简单的方式呢~当然了,这时候,你就需要一个支持上传的简单 HTTP Server,也就是我这个:SimpleHTTPServerWithUpload.py,哈哈。然后你开个服务,让人家上传即可。 其实这个就是修改自 SimpleHTTPServer 的,只不过我给它加上了最原始的上传功能,安全性方面没有验证过,不过理论上应该不会没人一直开着这个吧?另外,我对 RFC1867 的理解不一定透彻,所以,Use on your own risk! 截图如下: 代码在此,单文件、零配置,直接用 Python 运行。 #!/usr/bin/env python “"”Simple HTTP Server With Upload. This module builds on BaseHTTPServer by implementing the standard GET and HEAD requests in a fairly straightforward manner. “”” version = “0.1” all = [“SimpleHTTPRequestHandler”] author = “bones7456” home_page = “http://li2z.cn/” import os import posixpath import BaseHTTPServer import urllib import cgi import shutil import mimetypes import re try: from cStringIO import StringIO except ImportError: from StringIO import StringIO class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): “"”Simple HTTP request handler with GET/HEAD/POST commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. And can reveive file uploaded by client. The GET/HEAD/POST requests are identical except that the HEAD request omits the actual contents of the file. “”” server_version = “SimpleHTTPWithUpload/” + version def do_GET(self): “"”Serve a GET request.””” f = self.send_head() if f: self.copyfile(f, self.wfile) f.close() def do_HEAD(self): “"”Serve a HEAD request.””” f = self.send_head() if f: f.close() def do_POST(self): “"”Serve a POST request.””” r, info = self.deal_post_data() print r, info, “by: “, self.client_address f = StringIO() f.write(‘<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”>’) f.write(“<html>\n
原创文章,转载时请注明地址:http://zwssd1980.blog.163.com/blog/static/30296492011210102033425/ http://zwssdzwssd.appspot.com/log/83.html python 求1980年到2029年众数和 注:还不能求所有数的众数和,慢慢完善。 # -- coding: utf-8 -- import strin myyear = str(1980) for j in range(50): zsh = int(myyear[0])+int(myyear[1])+int(myyear[2])+int(myyear[3]) if zsh>10: zsh = str(zsh) zsh = int(zsh[0])+int(zsh[1]) print ‘年份:’+myyear+’众数和是:’+str(zsh) myyear = int(myyear) myyear += 1 myyear = str(myyear)
用python写的文件上传下载软件 登陆用的是主动模式 问题1:批量上传还没做好 (已经修改)问题2:下载还没改造好,会比较文件大小,但只修改了几个字符则会认为文件大小相同而不下载 2010年12月16日星期五 上午 11:07 修改好上传文件比较大小相同后不上传文件的问题,并且有进度显示。 2010年12月20日星期一 下午1:56 修改了登陆为一次登陆,上传多个文件后退出登陆。 # -- coding: utf-8 -- from ftplib import FTP import sys import os.path import string,datetime,time import socket class MyFTP(FTP): ‘’’’’ conncet to FTP Server ‘’’ def ConnectFTP(self,remoteip,remoteport,loginname,loginpassword): ftp=MyFTP() try: ftp.connect(remoteip,remoteport) except: return (0,’conncet failed’) else: try: ftp.set_pasv(0) ftp.login(loginname,loginpassword) except: return (0,’login failed’) else: return (1,ftp) def download(self,res,remotePath,localPath): #connect to the FTP Server and check the return #res = self.ConnectFTP(remoteHost,remotePort,loginname,loginpassword) if(res[0]!=1): print res[1] sys.exit() #change the remote directory and get the remote file size ftp=res[1] #ftp.set_pasv(0) dires = self.splitpath(remotePath) if dires[0]: ftp.cwd(dires[0]) remotefile=dires[1] print dires[0]+’ ‘+ dires[1] fsize=ftp.size(remotefile) if fsize==0 : return #check local file isn’t exists and get the local file size lsize=0L if os.path.exists(localPath): lsize=os.stat(localPath).st_size if lsize >= fsize: print ‘local file is bigger or equal remote file’ return blocksize=1024 cmpsize=lsize ftp.voidcmd(‘TYPE I’) conn = ftp.transfercmd(‘RETR ‘+remotefile,lsize) lwrite=open(localPath,’ab’) while True: data=conn.recv(blocksize) if not data: break lwrite.write(data) cmpsize+=len(data) print ‘\b’30,’download process:%.2f%%’%(float(cmpsize)/fsize100), lwrite.close() ftp.voidcmd(‘NOOP’) ftp.voidresp() conn.close() ftp.quit() def upload(self,res,remotepath,localpath,callback=None): if not os.path.exists(localpath): print “Local file doesn’t exists” return self.set_debuglevel(2) if res[0]!=1: print res[1] sys.exit() ftp=res[1] remote=self.splitpath(remotepath) ftp.cwd(remote[0]) rsize=0L try: rsize=ftp.size(remote[1]) except: pass if (rsize==None): rsize=0L lsize=os.stat(localpath).st_size if (lsize): localf=open(localpath,’rb’) ftp.voidcmd(‘TYPE I’) if (rsize==None): rsize = 0L datasock,esize=ftp.ntransfercmd(“APPE “+remote[1],0) else: datasock,esize=ftp.ntransfercmd(“STOR “+remote[1],0) cmpsize=0 while True: buf=localf.read(1024) if not len(buf): print ‘\rno data break’ break datasock.sendall(buf) if callback: callback(buf) cmpsize+=len(buf) print ‘\b’30,’uploading %.2f%%’%(float(cmpsize)/lsize100) if cmpsize==lsize: print remote[0]+remote[1]+’ 文件上传完成’ break datasock.close() #print ‘close data handle’ localf.close() #print ‘close local file handle’ ftp.voidcmd(‘NOOP’) #print ‘keep alive cmd success’ ftp.voidresp() #print ‘No loop cmd’ def closeftp(self,ftp): ftp.quit() print ‘现在退出FTP!’ def splitpath(self,remotepath): position=remotepath.rfind(‘/’) return (remotepath[:position+1],remotepath[position+1:]) def upload_files(self,remotehost,remoteport,loginname,loginpassword,remotepath,localpath,callback=None): if not os.path.exists(localpath): print “Local file doesn’t exists” return self.set_debuglevel(2) res=self.ConnectFTP(remotehost,remoteport,loginname,loginpassword) if not os.path.isdir(remotepath): return localnames = os.listdir(localdir) ftp.cwd(remotedir) for item in localnames: src = os.path.join(localdir, item) if os.path.isdir(src): try: ftp.mkd(item) except: self.debug_print(‘目录已经存在 %s’ %item) self.upload_files(src, item) else: self.upload_file(src, item) #ftp.cwd(‘..’) def debug_print(s): print (s) if name==’main’: lf = MyFTP() res=lf.ConnectFTP(‘...’,’21’,’username’,’password’) filename1 = ‘zhpj.php’ filename2 = ‘templates/newtheme/zhpj.html’ filename3 = ‘zhpj_ajax.php’ filename4 = ‘templates/newtheme/inc/pj.php’ filename5 = ‘templates/newtheme/inc/repj.php’ osdir = ‘/media/4C88-A7AD/work/yidaba/shop/’ ftpdir = ‘/ydbv10/shop/index/shop/’ lf.upload(res,ftpdir+filename1,osdir+filename1) lf.upload(res,ftpdir+filename2,osdir+filename2) lf.upload(res,ftpdir+filename3,osdir+filename3) lf.upload(res,ftpdir+filename4,osdir+filename4) lf.upload(res,ftpdir+filename5,osdir+filename5) lf.closeftp(res[1]) #lf.upload_files(‘...’,’21’,’username’,’password’,ftpdir,osdir) #暂时先不能用批量更新目录功能,可能会影响别人共同开发