How to send data in a looping with socket PYTHON

i need to send a data each every X seconds from a client to a server with SOCKET, so i try this code:

for i in range(100):
  localip = '127.0.0.1'
  port = 5010
  bufferSize = 1024
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
  client.connect((localip, port))

  pacote = '2B4F08FE0F3B'
  client.send(bytes(pacote, "utf-8"))

  client.close()

  time.sleep(10)

But in the serv i recive a package with nothing (‘ ‘). I need send 100x the same message to the serv, each 10 seconds. Somebody knows where is the mistake?

I have other Client.py where i put manually the data (with the input() ) and he works perfect, but now i need to do this automatically.

The client need to open a new connection to send each data.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

But in the serv i recive a package with nothing (‘ ‘).

This is because the socket gets closed after sending the message, i.e. client.close().

I need send 100x the same message to the serv, each 10 seconds. Somebody knows where is the mistake?

If the server expects the client to create a single connection and then send many data over this connection before closing it, then the client should do exactly this. Instead your client opens a new connection for each new message and then closes the connection immediately after the message.

This means either you must fix the server to match the clients behavior or fix the client to match the servers behavior.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x