[clug] MTP and Python - any ideas?

Chris Smart clug at christophersmart.com
Mon May 2 12:16:54 UTC 2016


On Mon, May 02, 2016 at 08:41:00PM +1000, Paul Wayper wrote:
>Hi all,
>
>Is anyone familiar with using libmtp to interface with MTP devices?  I'm
>trying to write a program to get some information from my phone, but while the
>kernel's usbfs module seems quiet happy to see the phone's file systems and
>MATE's file manager can browse the device, my simple "get the device name and
>free space" program doesn't get much further than the attempting to connect.
>
>The program is:
>
>#!/usr/bin/python
>
>import pymtp
>
># Connect to MTP
>mtp = pymtp.MTP()
>mtp.connect()
>
># Show a bit of data about the connection
>print "Device name:", mtp.get_devicename()
>print "Space:", mtp.get_freespace(), "of", mtp.get_totalspace(), "bytes free"
>
>But I get either:
>
>$ python phontosync.py
>Device 0 (VID=04e8 and PID=6860) is a Samsung Galaxy models (MTP).
>ignoring libusb_claim_interface() = -6PTP_ERROR_IO: failed to open session,
>trying again after resetting USB interface
>LIBMTP libusb: Attempt to reset device
>inep: usb_get_endpoint_status(): Resource temporarily unavailable
>outep: usb_get_endpoint_status(): Device or resource busy
>ignoring libusb_claim_interface() = -6LIBMTP PANIC: failed to open session on
>second attempt
>Traceback (most recent call last):
>  File "phontosync.py", line 7, in <module>
>    mtp.connect()
>  File "/usr/lib64/python2.7/site-packages/pymtp.py", line 514, in connect
>    raise NoDeviceConnected
>pymtp.NoDeviceConnected
>
>or:
>
>$ python phontosync.py
>Device 0 (VID=04e8 and PID=6860) is a Samsung Galaxy models (MTP).
>ignoring libusb_claim_interface() = -6PTP_ERROR_IO: failed to open session,
>trying again after resetting USB interface
>LIBMTP libusb: Attempt to reset device
>LIBMTP PANIC: failed to open session on second attempt
>Traceback (most recent call last):
>  File "phontosync.py", line 7, in <module>
>    mtp.connect()
>  File "/usr/lib64/python2.7/site-packages/pymtp.py", line 514, in connect
>    raise NoDeviceConnected
>pymtp.NoDeviceConnected
>
>(Not sure why I get the two different messages.)
>
>Any ideas?
>
>Thanks in advance,

OK, so I gave it a shot. I can confirm that I get the same error line
when I have my nexus phone mounted in nautilus and therefore it needs to
do the reset:

[22:04 chris /tmp]$ python ./mtptest.py
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc (for LG
Electronics/Samsung) Nexus 4/5/7/10 (MTP).
ignoring libusb_claim_interface() = -6PTP_ERROR_IO: failed to open
session, trying again after resetting USB interface
LIBMTP libusb: Attempt to reset device
Android device detected, assigning default bug flags

However, in my case the reset appears to be successful although it takes
between 1-3 minutes to print the results.

No error, if I make sure that nautilus isn't using the phone:

[22:13 chris /tmp]$ time python ./mtptest.py 
Device 0 (VID=18d1 and PID=4ee2) is a Google Inc (for LG
Electronics/Samsung) Nexus 4/5/7/10 (MTP+ADB).
Android device detected, assigning default bug flags
Device name: None
Space: 18808557568 of 58678456320 bytes free

real	0m59.668s
user	0m1.265s
sys	0m0.922s

pymtp just wraps libmtp but you can also install libmtp-examples and run
mtp-detect to take python out of the equation, at least.

You're probably running fedora, so in case it's helpful, this is what
I'm running (I did build libmtp 1.1.11 in case you want to try a newer
version, I have a spec):

[21:58 chris ~]$ uname -r
4.4.7-300.fc23.x86_64

[22:03 chris ~]$ rpm -q libmtp libmtp-examples python-pymtp
libmtp-1.1.9-2.fc23.x86_64
libmtp-examples-1.1.9-2.fc23.x86_64
python-pymtp-0.0.6-4.fc23.x86_64

[22:03 chris ~]$ sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      30


Maybe make sure you're not running with USB debugging and adb server in
the background.

Hope something there helps,
-c



More information about the linux mailing list