def serve(self): while self.running: try: (connectionSocket, address) = self.serverSocket.accept() except socket.timeout: continue self.sockFile = files.SocketFile(connectionSocket) done = not self.running while not done: try: msg = self.sockFile.readlineb() except files.ShutdownException: done = True except Exception as ex: try: errnum = int(ex[0]) if errnum != errno.ECONNRESET: self.printer.warnMsg( "Heartbeat monitor error getting beat (%s)" % ex) except: self.printer.warnMsg( "Heartbeat monitor error getting beat (%s)" % ex) done = True if done or len(msg) == 0: self.flush() done = True else: self.processBeatMessage(msg) done = not self.running
def startRequest(self, event, url, isFetch): id = event.id info = parseURL(url) if not info[0]: event.error(info[1]) return False host, port, uri = info[1:] event.addURI(uri) action = "Fetching" if isFetch else "Requesting" self.outMsg("%s '%s' from %s:%d" % (action, uri, host, port)) (phost, pport) = (host, port) if self.proxy is None else self.proxy tuples = socket.getaddrinfo(phost, pport, socket.AF_INET, socket.SOCK_STREAM) if len(tuples) == 0: event.error("Couldn't get address information for (%s:%d)" % (phost, pport)) return False sock = None for info in tuples: (family, socktype, proto, canonname, sockaddr) = info try: sock = socket.socket(family, socktype) sock.connect(sockaddr) except Exception as ex: sock = None msg = str(ex) continue break if sock is None: event.error("Couldn't connect to %s:%d (%s)" % (phost, pport, msg)) return False sockFile = files.SocketFile(sock) event.sockFile = sockFile event.url = url if self.verbose.getBoolean(): self.outMsg("Set up connection to %s:%d" % (phost, pport)) lines = [] lines.append("GET %s HTTP/1.0\r\n" % url) lines.append("Host: %s:%d\r\n" % (host, port)) lines.append("Request-ID: %s\r\n" % id) rtype = "Immediate" if isFetch else "Deferred" lines.append("Response: %s\r\n" % rtype) lines.append("Connection: close\r\n") lines.append("Proxy-Connection: close \r\n") lines.append("User-Agent: CMU/1.0 Iguana/20180704 PxyDrive/0.0.1\r\n") lines.append("\r\n") event.sentHeaderLines = lines header = "".join(lines) try: sockFile.write(header) except Exception as ex: event.error("Couldn't send request header for url %s (%s)" % (url, str(ex))) return False if self.verbose.getBoolean(): self.outMsg("Sent the following header") self.outMsg(header) return True
def run(self): if self.sock == None: self.errMsg("Server not enabled") return if self.verbose.getBoolean(): self.outMsg("Running server") while self.running: try: (conn, address) = self.sock.accept() except socket.timeout: continue self.requestCount += 1 if self.verbose.getBoolean(): self.outMsg("Connection request #%d from %s. Creating connection %s" % (self.requestCount, address, conn)) sockFile = files.SocketFile(conn) t = threading.Thread(target = self.wrappedHandleConnection, kwargs = {"sockFile" : sockFile}) t.start() self.sock.close()