2

Below is my server.py and client.py. So I want to print each item in the array separated by a new line. Server.py:

import socket

listener = socket.socket()
listener.bind(("",12345))
listener.listen()

while 1:
    conn = listener.accept()
    sock = conn[0]

    encoded_message = sock.recv(2048)
    while len(encoded_message) > 0:
    message += encoded_message.decode() + "\n"
    encoded_message = sock.recv(2048)

print(message)

encoded_message = message.encode()
sock.send(encoded_message)
sock.shutdown(1)

sock.close()

client.py:

import socket

sock = socket.socket()
sock.connect(("localhost",12345))

testing_strings = ["A123 45",
               "L65 78",
               "E486",
               "D4 1 0",
               "E1.0",
               "S1.0",
               "S0 2 -945 1689 -950 230 -25 1 -1e-15",
               "Not a number",
               "S0 2 -945 1689 -950 230 -25 1 0",
               "S0 2 -945 1689 -950 230 G 1 1e-15"]

for c in testing_strings:
    encoded_message = c.encode()
    sock.send(encoded_message)

sock.shutdown(1)

encoded_message = sock.recv(2048)
while len(encoded_message) > 0:
    message += encoded_message.decode()
    encoded_message = sock.recv(2048)

print(message)
sock.close()

But I always get

A123 45L65 78E486D4 1 0
E1.0
S1.0
S0 2 -945 1689 -950 230 -25 1 -1e-15
Not a number
S0 2 -945 1689 -950 230 -25 1 0
S0 2 -945 1689 -950 230 G 1 1e-15

It seems like the first three items always go together and the rest works just fine. I want to separate them out and print each line. I would appreciate any help. Thanks. Expected output:

A123 45
L65 78
E486
D4 1 0
E1.0
S1.0
S0 2 -945 1689 -950 230 -25 1 -1e-15
Not a number
S0 2 -945 1689 -950 230 -25 1 0
S0 2 -945 1689 -950 230 G 1 1e-15
3
  • post the final expected output Commented Oct 27, 2017 at 18:19
  • I just edited it. Thanks Commented Oct 27, 2017 at 18:25
  • can you show how are running your both scripts? Commented Oct 27, 2017 at 19:14

1 Answer 1

1

How each piece of message doesn't have the same size, so I think that the best way to accomplish this is some structured communication using serialization like XML, JSON, etc.

If you don't want use serialization, you can use a start and end tag or unique symbol, like a pipe "|", for each time you send the data. So, in the server code, you should concatenate all message and finally split the final content into a list.

separator = "|".encode()
for c in testing_strings:
    encoded_message = c.encode()
    sock.send(separator)
    sock.send(encoded_message)

I wrote an answer to a similar question, you can see here:

Simple Python Server Client File Transfer

I hope it helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey it works. Thanks a lot. Actually I'm trying to pass each item in the array to calculate the polynomials. And I was testing to see how the data come in.Now I know it works fine. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.