Bottom Page

Thread Rating:
  • 4 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP/IP client script help
#1
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?
Quote
#2
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.
Rehan11 likes this post
Quote
#3
(Jan-14-2019, 07:44 PM)nilamo Wrote: 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.
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 ?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Graphql subscriptions client Edoar 0 53 Mar-11-2019, 04:13 PM
Last Post: Edoar
  recive post request from client(browser) python sockets kunz 1 353 Dec-13-2018, 12:53 AM
Last Post: kunz
  tcp server/client port connection issue valshev 2 626 Sep-01-2018, 02:50 PM
Last Post: valshev
  Frames above 2000 bytes not acknowledged by client/receiver lukasz139 0 378 Aug-24-2018, 06:34 PM
Last Post: lukasz139
  Send data BMP180 between client and server trought module socket smalhao 0 487 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 538 Jul-30-2018, 07:52 AM
Last Post: Lyperion
  need to understand better __setup__.py, websocket-client, packaging penright 0 496 Jul-08-2018, 09:40 PM
Last Post: penright
  Need help to make a server/client for DNS Eddie05 1 643 Jun-12-2018, 10:04 AM
Last Post: j.crater
  Flask.socketio and client side html tables / JS georgelza 1 830 Jun-05-2018, 03:56 AM
Last Post: georgelza
  Looking for IBM MQ client for MAC to go with PyMQi georgelza 1 816 May-08-2018, 03:04 PM
Last Post: georgelza

Forum Jump:


Users browsing this thread: 1 Guest(s)