python for android: pad 从PC接收文件
2013-12-29 14:39
471 查看
如果不用USB线, pad 怎样从PC接收文件 ? 我在家用无线路由器, pad 用WiFi 连网.
Windows PC 上先启动服务程序 getfile.py -mode server
cmd 用 ipconfig 看PC的IP地址.
android pad 运行 getfile1.py
Windows PC 上先启动服务程序 getfile.py -mode server
import sys, os, thread, time from socket import * def now(): return time.strftime('%Y-%m-%d %X',time.localtime()) bufsz = 1024 defaultHost = 'localhost' defaultPort = 55555 path = '/python4android/' helptext = """ Usage... server=> getfile.py -mode server [-port nnn] [-host hhh|localhost] client=> getfile.py [-mode client] -file fff [-port nnn] [-host hhh|localhost] """ def parsecommandline( ): dict = {} # put in dictionary for easy lookup args = sys.argv[1:] # skip program name at front of args while len(args) >= 2: # example: dict['-mode'] = 'server' dict[args[0]] = args[1] args = args[2:] return dict def client(host, port, filename): sock = socket(AF_INET, SOCK_STREAM) sock.connect((host, port)) sock.send(filename + '\n') # send remote name with dir file = open(filename, 'wb') # create local file in cwd while True: data = sock.recv(bufsz) # get up to 1K at a time if not data: break # till closed on server side file.write(data) # store data in local file sock.close( ) file.close( ) print 'Client get', filename, 'at', now( ) def serverthread(clientsock): global path sockfile = clientsock.makefile('r') # wrap socket in dup file obj filename = sockfile.readline( )[:-1] # get filename up to end-line dropdir = path + os.path.split(filename)[1] try: file = open(dropdir, 'rb') while True: bytes = file.read(bufsz) # read/send 1K at a time if not bytes: break # until file totally sent sent = clientsock.send(bytes) assert sent == len(bytes) file.close() print 'download file :', dropdir except: print 'Error download file on server:', filename clientsock.close( ) def server(host, port): serversock = socket(AF_INET, SOCK_STREAM) # listen on TCP/IP socket serversock.bind((host, port)) # serve clients in threads serversock.listen(5) while True: clientsock, clientaddr = serversock.accept( ) print 'Server connected by', clientaddr, 'at', now( ) thread.start_new_thread(serverthread, (clientsock,)) def main(args): host = args.get('-host', defaultHost) # use args or defaults port = int(args.get('-port', defaultPort)) # is a string in argv if args.get('-mode') == 'server': # None if no -mode: client if host == 'localhost': name = gethostname() host = gethostbyname(name) # else fails remotely print host,port server(host, port) elif args.get('-file'): # client mode needs -file client(host, port, args['-file']) else: print helptext if __name__ == '__main__': args = parsecommandline( ) main(args)
cmd 用 ipconfig 看PC的IP地址.
android pad 运行 getfile1.py
# -*- coding: utf8 -*- import android import sys, os, time from socket import * def now(): return time.strftime('%Y-%m-%d %X',time.localtime()) droid = android.Android() filename = droid.dialogGetInput(u"从PC接收文件",u"请输入文件名:").result print filename bufsz = 1024 host = '192.168.0.103' port = 55555 path = '/mnt/sdcard/sl4a/scripts/' sock = socket(AF_INET, SOCK_STREAM) sock.connect((host, port)) sock.send(filename + '\n') # send remote name with dir file = open(path+filename, 'wb') # create local file in cwd while True: data = sock.recv(bufsz) # get up to 1K at a time if not data: break # till closed on server side file.write(data) # store data in local file sock.close( ) file.close( ) print 'Client get', filename, 'at', now( )在android 4.1 pad 上测试通过.
相关文章推荐
- python for android : 手机从PC接收文件
- python for android : 一个简单文件浏览器的实例 play mp3
- Python for Android,将你的Python应用打包为APK文件
- iOS接收Androidbase64文件时需要处理文件格式以免崩溃
- android手机客户端上传文件,java servlet服务器端接收并保存到服务器
- 解决Visual Stuido 2012中Mono For Android *.axml文件没有智能提示问题
- zz:Android中发送Http请求实例(包括文件上传、servlet接收)
- Python for Windows——监控Windows某个目录下文件的变化
- 请教,Android端提交图片文件后,服务器端采用thinkphp该如何接收?
- python for android : BeautifulSoup + ListView
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(二,发送彩信<2>)
- mono for android Json 上传文件
- 〖Android〗sshd for android, 及映射根文件系统至本地盘符
- 用Python解析AndroidManifest.xml文件找MainActivity
- Mono For Android 4.4.54 Windows Crack 破解文件
- Android QQ for Pad修改尾巴信息教程
- Android 蓝牙开发(五)OPP接收文件
- PC通过ADB命令获取Android制定文件内容并保存在本地
- python for android : 读通讯录
- 非阻塞socket实现android手机与PC的文件传输