I forgot this: // Manage connection for different behavior func handleConn(conn net.Conn, binPath string) {
msg := "Status?\n" if binPath != "" { // Execute command and send Standard I/O net.Conn cmd := exec.Command(binPath) cmd.Stdin = conn cmd.Stdout = conn cmd.Stderr = conn cmd.Run() fmt.Println("Disconnected!?") time.Sleep(5 * time.Second) pippo, err2 := conn.Write([]byte(msg)) if err2 != nil { fmt.Println(pippo) } } else { // Copy Standard I/O in a net.Conn go io.Copy(os.Stderr, conn) go io.Copy(os.Stdout, conn) io.Copy(conn, os.Stdin) } } Il giorno martedì 29 giugno 2021 alle 12:27:00 UTC+2 LetGo ha scritto: > I have a proxy written in python with some logic that I would like to > implement in a golang tool of mine, but I don't really know how to do it. > It splits the data sent via socket (stdout) in small chunks, with a delay > between each other > > I have a variable which gets a random number from a list: > .... > listbytes = [87, 88, 89, 90] > .... > > I have also a function which splits in small chunk of bytes(n) the > data(lst): > > ............ > > def chunks(lst, n): > > "Yield successive chunks from lst, where n is a list of possible sizes" > > i = 0 > > while i < len(lst): > > k = min(random.choice(n), len(lst) - i) > > yield lst[i:i + k] > > i += k > ............. > > Both things are executed this way: > ... > # get the data > data = s.recv(BUFFER_SIZE) > > if s == s_src: > d = LOCAL_DATA_HANDLER(data) > for chunk in chunks(d, listbytes): > #sleep 1.6 seconds before sending the next chunk of data > time.sleep(1.6) > #send the chunked data > s_dst.send(bytes(chunk) ) > ... > > > How do I implement *the same exact logic* illustrated here? > > func (nObj NetObject) RunClient(cmd string) { > // Try connection > > for { > conn, err := net.Dial(nObj.Type, nObj.Service) > fmt.Print("connect") > // msg := "status" > if err != nil { > fmt.Println("fail") > } > if err == nil { > fmt.Println("ok") > // defer conn.Close() > defer conn.Close() > log.Println("Connected to", conn.RemoteAddr()) > handleConn(conn, cmd) > > > //handleConn(conn, cmd) > fmt.Println("After handle") > } > fmt.Println("Before sleep") > time.Sleep(5 * time.Second) > } > > } > Can you help me please? > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/43b6d2ef-ab65-4d07-b8d8-ff814c12e8cen%40googlegroups.com.