Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No response from socket
#1
Greetings,

The script below works well for one of my embedded sensing systems:

import socket

TCP_IP = 'X.X.X.X'
TCP_PORT = MY_PORT
BUFFER_SIZE = 1024
MESSAGE = "MY_MESSAGE"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
MESSAGE = MESSAGE.encode('utf8')
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print("received data:", data)
For another one, I was unable to connect to it using the same approach. The script below shows another way to do this:
import socket

TCP_IP = 'X.X.X.X'
TCP_PORT = MY_PORT
BUFFER_SIZE = 1024

s = socket.create_connection((TCP_IP, TCP_PORT))

MESSAGE = "MY_MESSAGE"
MESSAGE = MESSAGE.upper()
MESSAGE = MESSAGE.encode("utf-8")
s.sendall(MESSAGE)

while True:
    data = s.recv(BUFFER_SIZE)
    
    if not data:
        break
    
    print("Received data:", data)
    print("Data string:", data.decode("utf-8"))

'''
data = s.recv(BUFFER_SIZE)
print("Received data:", data)

if data:
    datastring = data.decode("utf-8")
    print("Data string:", datastring)

s.close()
'''
Somehow, I don't receive any response back... Any ideas why?
Reply
#2
Please use proper code tags while posting your thread. See BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020