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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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} ))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
You could program the application to "listen" for specific key sequences and perform a method. For example, the VB 6 equivalent of KeyPreview
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Printers and Scanners

From novice to tech pro — start learning today.