def HandleUninitialized(self, socket): json = socket.ReadCallback() stream = socket.HandleStream(json) self.uninitialized.remove(socket) socket.Socket().send(json) ##### if socket in self.outputs: self.outputs.remove(socket) self.inputs.remove(socket) del self.message_queues[socket] del self.registry[socket.Socket()] self.RegisterDataStream(stream)
def HandleExceptional(self, socket): inputs.remove(socket) if socket in self.outputs: self.outputs.remove(socket) socket.Close() del message_queues[socket] del self.registry[socket.Socket()]
def HandleWritable(self, socket): try: next_msg = self.message_queues[socket].get_nowait() except queue.Empty: self.outputs.remove(socket) else: socket.Socket().send(next_msg)
def fetch(url): sock = socket.Socket() sock.connect(('xkcd.com', 80)) request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url) sock.send(request.encode('ascii')) response = b'' chunk = sock.recv(4096) while chunk: response += chunk chunk = sock.recv(4096) # Page is now downloaded links = parse_links(response) q.add(links)
def HandleReadStream(self, socket): name = socket.Name() data = socket.ReadCallback() result = socket.HandleStream(data) if result is not None: self.engine.ExecutePlugin(name, result) if data: #print(sys.stderr, 'received', data, 'from', socket.Socket().getpeername()) self.message_queues[socket].put(str(len(data)).encode()) if socket not in self.outputs: self.outputs.append(socket) else: print(sys.stderr, 'closing', socket.Address) if socket in self.outputs: self.outputs.remove(socket) self.inputs.remove(socket) del self.message_queues[socket] del self.registry[socket.Socket()]
def thread_func(lock): # LF = open(u'c:\\logfile.txt', 'w') # lmsg = "init" # LF.write(lmsg) import socket aSSrv = socket.SocketServ() aSSrv.Connect(5) aSocket = socket.Socket() # lmsg = "\nsocket server open" # LF.write(lmsg) try: aSocket.Open(aSSrv, 0x0800, 1, 6) # lmsg = "\nsocket opened" # LF.write(lmsg) except: pass try: # aSocket.Connect('216.239.59.99', 80) #google aSocket.Connect('193.166.223.20', 80) #fmi # lmsg = "\nsocket connected" # LF.write(lmsg) except: pass try: aSocket.Write("GET /index.html\x0d\x0a") # lmsg = "\nWRITE called" # LF.write(lmsg) except: pass GENERR = 0 RECEIVED = 0 msg = '' while (RECEIVED ==0): try: chunk = " " # i=0 while len(chunk) > 0: chunk = aSocket.RecvOneOrMore() msg = msg + chunk # i=i+1 # lmsg = "\nRecvOneOrMore called" # LF.write(lmsg) # LF.write("\nlast chunk is:\n") # LF.write(chunk) # LF.write("\nIterations: ") # LF.write(i) RECEIVED = 1 GENERR = 0 #first time it usually fails except: # LF.write("\nERROR in receiving") GENERR = 1 RECEIVED = 0 if GENERR != 1: try: f = open(u'c:\\downloaded.html', 'w') f.write(msg) f.close() # lmsg = "\nother file operations" # LF.write(lmsg) except: pass # LF.close() del aSocket del aSSrv lock.signal()
#!/usr/bin/python2 import socket s = socket.Socket() host = socket.gethostname() port = 12345 s.connect(host, port) print s.recv(1024) s.close
def GetRawSocketList(self, sockets): return [socket.Socket() for socket in sockets]