#!/usr/bin/python # # Example TCP server. Will listen on a specified port forever. If a client # connects, feed them the current reading from the thermocouple and then # close the socket. # # Eli Fulkerson. # from socket import * from select import * import d5000_serial HOST = '' PORT = 80 class Connection: """ An accepted connection. """ def __init__(self, socket, address): self.socket = socket self.ip = address[0] def sendline(self, data): self.socket.send(data + "\r\n") def fileno(self): return self.socket.fileno() class ConnectionHandler: """ Maintains all connections. """ def __init__(self): self.listener = socket(AF_INET, SOCK_STREAM) self.listener.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.listener.bind((HOST, PORT)) self.listener.listen(32) self.sensor = d5000_serial.sensor("-p 0 -r 9600") self.module = d5000_serial.module(self.sensor, 1) print "Thermocouple server is active and listening on port " + `PORT` + "." def run(self): """ Listen for incoming connections on PORT. When a connection is recieved, send a 302 pull a reading from the sensor and close the socket. """ # Accept an incoming connection. conn = select( [self.listener], [], [], 0.1)[0] if conn: socket, address = self.listener.accept() conn = Connection(socket, address) # burn some time so that the client and server don't have a timing error #trash = conn.socket.recv(1024) data = self.module.sendcmd("RD") # send our https redirect conn.sendline(data) # kick em out conn.socket.close connections = ConnectionHandler() while 1: connections.run()