TCP/IP client script help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: TCP/IP client script help (/thread-15304.html) |
TCP/IP client script help - Rehan11 - Jan-12-2019 Client script Here's simple code to send and receive data by TCP in Python: #!/usr/bin/env python import socket TCP_IP = '192.168.0.12' TCP_PORT = 5004 BUFFER_SIZE = 1024 MESSAGE = "How are you!" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) s.send(MESSAGE) data = s.recv(BUFFER_SIZE) s.close() print ("received data:", data)I want to send the message "How are you!" continuously to the server. the server would respond either "I am fine" or "I am not okay" if the server responds " I am fine " do nothing but if the server responds "I am not okay" then print the message " I will take care of you" How to make python client script for the above purpose? RE: TCP/IP client script help - nilamo - Jan-14-2019 You currently send it once. To send it repeatedly, use a loop. You currently get and display the response. Use an if statement to check it's result. RE: TCP/IP client script help - Rehan11 - Jan-15-2019 (Jan-14-2019, 07:44 PM)nilamo Wrote: You currently send it once. To send it repeatedly, use a loop.Thanks nilamo Do you mean like this #!/usr/bin/env python import socket TCP_IP = '192.168.0.12' TCP_PORT = 5004 BUFFER_SIZE = 1024 MESSAGE = "How are you!" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) while True: s.send(MESSAGE) data = s.recv(BUFFER_SIZE) data2 = data if data2 != data: print(" I will take care of you") s.close()I know I have to store and compare previous and present string does it make any sense ? |