Created
July 27, 2014 18:06
-
-
Save caetanus/6f07578f8d2d53c33139 to your computer and use it in GitHub Desktop.
LPR client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import socket | |
import select | |
class RSerialError(IOError): | |
pass | |
class RSerial(object): | |
def __init__(self): | |
self.connect() | |
def connect(self): | |
self.conn = socket.socket() | |
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
udp.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) | |
udp.sendto("gimme your address, please", ("255.255.255.255", 1235)) | |
if not select.select([udp], [], [], 1): | |
raise RSerialError("couldn't discover the server address.") | |
_, addr_and_port = udp.recvfrom(2048) | |
addr = addr_and_port[0] | |
self.conn.connect((addr, 9100)) | |
def write(self, data): | |
try: | |
self.conn.send(data) | |
except socket.error, e: | |
self.connect() | |
self.write(data) | |
def __del__(self): | |
self.conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment