How to accept input from USB barcode Reader with Python


I am trying to create a program that will accept input from a usb barcode reader and then perform an HTML request to a website forming a url based on the input

eg (

Looking around I found out that using pyusb library, I can lock a specific USB device and accept input from it.

Also using urllib2.urlopen i can perform the html request.

Firstly, how can I make the python script to continuously wait for user input ( the first barcode card is scanned, the url is formed and then again make the script return to wait for user input? )

Secondly, is it possible to create an executable of that script. Not having experience with python I searched and found that there is py2exe. Because this script depends on 3rd party libraries, is py2exe going to automatically package these libraries eg pyusb ( windows installation says that for pyusb to work you need cygwin ...? does that still makes sense if i develop and package under linux) or do i have to do something specific.


PS: if anyone else is aware of other libraries or a better way of doing what I am trying please let me know.
Who is Participating?
clockwatcherConnect With a Mentor Commented:
Are you positive your USB barcode reader doesn't show up as HID device and act as a keyboard?  Pretty sure you'll find that you don't need to use PyUSB and that the barcode reader will work as if the scanned data is simply being typed in via the keyboard.

import sys
import urllib

while 1:
    line = sys.stdin.readline().rstrip()
    f = urllib.urlopen("" % urllib.urlencode( {'q':line} ))
vlachosgAuthor Commented:
thanks for that. I am aware that usb barcode scanners act as keyboard input the problem though is that in the computer that this program is going to be installed the python program will run in the background. So for example when someone is typing something on a texteditor the input is not going to be transferred to the python program as well, is this correct?
tncodeConnect With a Mentor Commented:
You could program the application to "listen" for specific key sequences and perform a method. For example, the VB 6 equivalent of KeyPreview
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

So the scanner is dedicated to your app alone and you want it available regardless of whatever else is going on with the computer at the time?  

In that case, maybe libusb and pyusb will be your best bet.   Not sure if Windows 7 is applicable to you or not, but I have doubts that you'll be able to get a libusb-win32 device driver installed under it since Microsoft cut out support for loading non-signed drivers (in non Test Mode).   It may not be applicable to a libusb-win32 filter-based driver (if that's the direction you end up going) but guessing that it will be--  don't see how the filter driver won't have to have some kernel mode component involved.

If this is windows only, raw input would probably be Microsoft's answer for how to do what it sounds like you want to do--

A C# example:
vlachosgAuthor Commented:
ok so thats what I decided to do after some research. I will write a key logger with pyhook and also add in the barcode card some prefix eg( bc00c ). So when the input from the barcode (acting as keyboard) is entered the key logger will check letter by letter and if it matches the prefix then it will accept the rest as input and process it accordingly.

I guess that is the best choice because it does not tie the bar-code reader to a specific application.

vlachosgAuthor Commented:
Not completely covered the questions asked.
All Courses

From novice to tech pro — start learning today.