Python for android: receives files from the PC

Source: Internet
Author: User

The script getfile1.py used by pad to receive files from PC

In this article, the android mobile phone receives files from the PC using the script getfile2.py, which is easier to use than getfile1.py.

#-*-Coding: utf8-*-import androidimport sys, OS, timefrom socket import * droid = android. android () def now (): return time. strftime ('% Y-% m-% d % x', time. localtime () if droid. checkWifiState (). result = False: print 'check Wifi state' sys. exit (4) base_dir = '/mnt/sdcard/sl4a/scripts/' if not OS. path. isdir (base_dir): print base_dir, 'is not dir' sys. exit (4) def show_dir (path = base_dir): "" Shows the conte ETS of a directory in a list view. "# The files & directories under" path ". nodes = sorted (OS. listdir (path) # Make a way to go up a level. if path! = Base_dir: nodes. insert (0 ,'.. ') droid. dialogCreateAlert (OS. path. basename (path ). title () droid. dialogSetItems (nodes) droid. dialogShow () # Get the selected file or directory. result = droid. dialogGetResponse (). result droid. dialogDismiss () if 'item' not in result: return target = nodes [result ['item'] target_path = OS. path. join (path, target) if target = '.. ': target_path = OS. path. dirname (path) if OS. path. isdir (target_path): show_dir (target_path) elif OS. path. splitext (target) [1]. lower () = '. py': return target_path # inform the user. else: droid. makeToast ('only. py files are currently supported! ') Show_dir (path) path = show_dir () filename = droid. dialogGetInput (u "receiving files from PC", OS. path. dirname (path), OS. path. basename (path )). resultif filename is not None: path = OS. path. dirname (path) + '/' + filenameprint pathbufsz = 1024 host = '2017. 168.0.103 'port = 55555 sock = socket (AF_INET, SOCK_STREAM) sock. connect (host, port) sock. send (filename + '\ n') # send remote name with dirfile = open (path, 'wb') # create local file in cwdwhile True: data = sock. recv (bufsz) # get up to 1 K at a time if not data: break # till closed on server side file. write (data) # store data in local filefile. close () sock. close () print 'client get', filename, 'at', now ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.