champ1
Posts: 47
Joined: Tue Jul 17, 2018 12:36 am

python tcp client script help

Mon Jan 14, 2019 12:04 pm

Here's simple code to send and receive data by TCP in Python:

python tcp client script

Code: Select all

#!/usr/bin/env python
 
import socket
 
TCP_IP = '192.168.0.2'
TCP_PORT = 5006
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?

User avatar
MrYsLab
Posts: 183
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: python tcp client script help

Mon Jan 14, 2019 4:21 pm

You should be able to adapt an echo client/server to meet your needs. Here is a link that may get you going:
https://pymotw.com/3/socket/tcp.html

champ1
Posts: 47
Joined: Tue Jul 17, 2018 12:36 am

Re: python tcp client script help

Tue Jan 15, 2019 10:10 am

MrYsLab wrote:
Mon Jan 14, 2019 4:21 pm
You should be able to adapt an echo client/server to meet your needs. Here is a link that may get you going:
https://pymotw.com/3/socket/tcp.html
I am trying to make a client script that can send message continuously and able to compare previous and current data

Does my script send a message continuously?
Does it compare previous and current string ?

Code: Select all

#!/usr/bin/env python
import socket
 
TCP_IP = '192.168.0.2'
TCP_PORT = 5006
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 data != data2:
	     print(" I will take care of you") 
	  
s.close()
 

User avatar
MrYsLab
Posts: 183
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: python tcp client script help

Tue Jan 15, 2019 1:34 pm

Here is another link https://wiki.python.org/moin/TcpCommunication that shows both sides of the connection. You will need to add a while loop and remove the close() to keep both sides running continuously.

champ1
Posts: 47
Joined: Tue Jul 17, 2018 12:36 am

Re: python tcp client script help

Wed Jan 16, 2019 11:32 am

MrYsLab wrote:
Tue Jan 15, 2019 1:34 pm
Here is another link https://wiki.python.org/moin/TcpCommunication that shows both sides of the connection. You will need to add a while loop and remove the close() to keep both sides running continuously.
Do you think concept wise I am correct for client script ?

User avatar
MrYsLab
Posts: 183
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: python tcp client script help

Wed Jan 16, 2019 1:12 pm

Your concept seems sound, but I have not tried your scripts to know if the code is complete. If you look at either of the articles that I have pointed to, you should be able to get things up and running quickly. In the second article, it sends a message once and closes. As I mentioned earlier, remove the close and insert a "While True'" to keep sending either the same block or some modified. If you cut and paste the code in that article and run the server on one terminal and the client in the other, you will see that the code does work.

One of the best ways of learning something new in programming is to hack a working example. I am not trying to give you a hard time, but I have pointed you to working code, and it is now your turn to hack it into something that meets your needs.

If you have questions about it or your hacking efforts, please feel free to post and I will try to answer your questions. If you run into a problem that you do not understand, again, please feel free to ask questions.

hippy
Posts: 4225
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: python tcp client script help

Wed Jan 16, 2019 5:24 pm

champ1 wrote:
Tue Jan 15, 2019 10:10 am

Code: Select all

      data = s.recv(BUFFER_SIZE)
      data2 = data	  
      if data != data2:
	     print(" I will take care of you")
The second line makes 'data2' equal to 'data' so they will never be different when the third line is reached.

You probably want to move that second line to after the 'if' and 'print', and you will have to then initialise 'data2' before it is first used in that 'if' comparison.

champ1
Posts: 47
Joined: Tue Jul 17, 2018 12:36 am

Re: python tcp client script help

Sat Jan 19, 2019 12:27 pm

hippy wrote:
Wed Jan 16, 2019 5:24 pm
The second line makes 'data2' equal to 'data' so they will never be different when the third line is reached.

You probably want to move that second line to after the 'if' and 'print', and you will have to then initialise 'data2' before it is first used in that 'if' comparison.
ok do you like

Code: Select all

#!/usr/bin/env python
import socket 
TCP_IP = '192.168.0.2'
TCP_PORT = 5006
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)      
	  
      if data != data2:
	     print(" I will take care of you") 
		 
      data2 = data
	  

hippy
Posts: 4225
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: python tcp client script help

Sat Jan 19, 2019 2:13 pm

champ1 wrote:
Sat Jan 19, 2019 12:27 pm
ok do you like ...
Better, but you will get an "undefined data2 variable" or similar error when you hit the 'if' test the first time because 'data2' has not been initialised before then.

champ1
Posts: 47
Joined: Tue Jul 17, 2018 12:36 am

Re: python tcp client script help

Sat Jan 19, 2019 2:54 pm

hippy wrote:
Sat Jan 19, 2019 2:13 pm

Better, but you will get an "undefined data2 variable" or similar error when you hit the 'if' test the first time because 'data2' has not been initialised before then.
How to initialise 'data2' ?

Code: Select all

#!/usr/bin/env python
import socket 
TCP_IP = '192.168.0.2'
TCP_PORT = 5006
BUFFER_SIZE = 1024
MESSAGE = "How are you!"
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
data2 = "  "
while True:
      s.send(MESSAGE)
      data = s.recv(BUFFER_SIZE)      
	  
      if data != data2:
	     print(" I will take care of you") 
		 
      data2 = data
	  

Return to “Python”