# Accessing USB input in container in python

**URL:** <https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946>\
**Category:** Product support\
**Tags:** raspberrypizero\
**Created:** [October 2, 2019, 8:55am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946 "2019-10-02T08:55:49Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [October 3, 2019, 2:05pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/26 "2019-10-03T14:05:30Z")

</div>

Could you also share the snippet you use to read data from the device?  
It may help analyzing and reproducing the problem?

---

<div class="post-metadata">

**Author:** ![junett](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@junett](https://forums.balena.io/u/junett)\
**Post date:** [October 4, 2019, 7:07am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/28 "2019-10-04T07:07:05Z")

</div>

Hi @roman-mazur,

I have a created a repo which replicates my issue: [https://github.com/Junett/VLP](https://github.com/Junett/VLP).

Thanks,

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [October 4, 2019, 11:28am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/29 "2019-10-04T11:28:09Z")

</div>

Thanks for sharing!

If I read your Python code directly, it attempts to read from standard input. However, there is nothing that would connect your python process standard input to the actual device.  
You mentioned that your scanner device is available as `/dev/hidraw0` from the container.  
In this case, assuming this device driver is implemented in a way that allows reading data from that file, I would attempt reading from that file directly in your Python code with something like

```auto
with open('/dev/hidraw0') as f:
  line = f.readLine()

```

If you prefer keeping your python code in a form when it accesses std input instead of a specific file, you will need to make a pipe between that device stream and your process, wrapping your container entry point with a script similar to

```auto
cat /dev/hidraw0 | python3 main.py

```

(you would need to add such script to your `Dockerfile` and set it as CMD on the image)

Again, this might work if the device driver allows reading from the device file like `cat /dev/hidraw0`.

---

<div class="post-metadata">

**Author:** ![junett](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@junett](https://forums.balena.io/u/junett)\
**Post date:** [October 4, 2019, 1:14pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/33 "2019-10-04T13:14:21Z")

</div>

Hi [@roman-mazur](https://forums.balena.io/u/roman-mazur),

I’ve just tried:

> with open(’/dev/hidraw0’) as f: line = f.readLine()

But I still had the same result. I will continue to look into ways to read direct from the device as this would probably be the best approach.

Thanks for your help, it is appreciated!

---

<div class="post-metadata">

**Author:** ![hedss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/hedss/32/210_2.png) [@hedss](https://forums.balena.io/u/hedss)\
**Post date:** [October 4, 2019, 1:22pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/34 "2019-10-04T13:22:54Z")

</div>

Hi @junett ,

Whilst it’s not python, we’ve a NodeJS example that works that might be useful, which uses a USB HID library to read from a USB HID device given a vendor and product ID. That’s here: [https://github.com/balena-io-playground/node-keyboard-input](https://github.com/balena-io-playground/node-keyboard-input)

Hopefully this will help!

Best regards,

Heds

---

<div class="post-metadata">

**Author:** ![MydKnight](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mydknight/32/2619_2.png) [@MydKnight](https://forums.balena.io/u/MydKnight)\
**Post date:** [October 10, 2019, 6:02am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/36 "2019-10-10T06:02:23Z")

</div>

I am having much the same problem as the above user. I’m using code that works fine in standalone, my usb device detects as a keyboard and works without issue, but a call to n = input() results in nothing being read when the USB device is hit. I dont usually do readline, but raw\_input or input now on python 3, so didnt ry with open, but I could. I was trying to figure out how to do the cat /dev/hidraw0 (which my device also shows up as) piping it to python3 and my program name, but that one failed as well saying cat not found…so whatever I did in the docker image was clearly the wrong thing. If it would help I could provide access and the UUID as well.

I have put in 1 day of access to uuid: f751d7c0616a7644a57686d47b652aec

Any help greatly appreciated!

---

<div class="post-metadata">

**Author:** ![junett](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@junett](https://forums.balena.io/u/junett)\
**Post date:** [October 10, 2019, 6:39am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/37 "2019-10-10T06:39:02Z")

</div>

Hi @MydKnight,

I tried multiple different ways of trying to read the input but still couldn’t get any to work. I never tried this method:

> [@roman-mazur](#):
>
> ```auto
> cat /dev/hidraw0 | python3 main.py
> 
> ```

I wasn’t sure exactly where to put it, I wasn’t sure if it was meant to be in the Dockerfile or a Docker container was required.

If you do find a solution, I would appreciate if you could post it on this thread as I am still stuck!!

Thanks!

---

<div class="post-metadata">

**Author:** ![MydKnight](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mydknight/32/2619_2.png) [@MydKnight](https://forums.balena.io/u/MydKnight)\
**Post date:** [October 10, 2019, 2:50pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/43 "2019-10-10T14:50:13Z")

</div>

yeah, I intellectually understand what that thing says its doing, but im not entirely certain how to implement the above. Hopefully whoever asked you for remote access to your application container can get into mine and see something we’ve missed.

---

<div class="post-metadata">

**Author:** ![MydKnight](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mydknight/32/2619_2.png) [@MydKnight](https://forums.balena.io/u/MydKnight)\
**Post date:** [October 10, 2019, 5:35pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/44 "2019-10-10T17:35:27Z")

</div>

Had some local confusion on my device so its reimaged. Support access granted for UUID feceb8ba298dd234563030eaa9bda4a9

---

<div class="post-metadata">

**Author:** ![MydKnight](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mydknight/32/2619_2.png) [@MydKnight](https://forums.balena.io/u/MydKnight)\
**Post date:** [October 10, 2019, 8:51pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/49 "2019-10-10T20:51:45Z")

</div>

> [@junett](#):
>
> readLine

So, this sucks, but it works at least at a base level:

```
dev = os.open("/dev/usb/hiddev0", os.O_RDONLY) 

```

Then you read it with:

```
n = os.read(dev, 10)

```

This allows you to read the raw input values for the usb port. There HAS to be a better option though.

---

<div class="post-metadata">

**Author:** ![samothx](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@samothx](https://forums.balena.io/u/samothx)\
**Post date:** [October 11, 2019, 10:53am UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/54 "2019-10-11T10:53:01Z")

</div>

Hi @mydknight ,  
a collegue suggested this solution ( [https://github.com/shaunmulligan/resin-keyboard-example](https://github.com/shaunmulligan/resin-keyboard-example) ) to read keyboard entries in a container. I have given it a try as he suggested it is a bit old and might not work any more but it works for me. I can see keyboard input in the journal.

---

<div class="post-metadata">

**Author:** ![MydKnight](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mydknight/32/2619_2.png) [@MydKnight](https://forums.balena.io/u/MydKnight)\
**Post date:** [October 11, 2019, 7:57pm UTC](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946/57 "2019-10-11T19:57:38Z")

</div>

After some digging around, I installed evdev, which seems to let me connect to a usb port and capture its input. Theres some hoop jumping to go from that to useable keyboard output, but id say I got 90 percent there from someones code using a barcode scanner. Seems to be working just fine now. Message me if you need more.

[Previous page](https://forums.balena.io/t/accessing-usb-input-in-container-in-python/34946.md?page=1)
