2

I have three files. client.py, server.py and test.py. client.py is below:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 6088))
while True:
    a = raw_input()
    s.send(a)
s.close()

server.py:

import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1',6088))
s.listen(1)
print("Waitting for connection.....")
while True:
    socket,addr = s.accept()
    while True:
        data = socket.recv(1024)
        if data:
           print(data)
s.close()

test.py:

while True:
    a = raw_input()
    print("welcom "+ a)

I use server.py or test.py to start the server, then I use the client.pyto start the client, but when I am inputting something in client, the server doesn't show anything. Why? And how to fix it.

Mahir Islam
  • 1,941
  • 2
  • 12
  • 33
FledgeXu
  • 164
  • 1
  • 9
  • Have you tried to use unbuffered output, i.e. `python -u server.py | ....` ? – Steffen Ullrich Jul 11 '18 at 03:20
  • Correct me if I'm wrong, but can we assume you see the server printing messages if you don't pipe its output to the test script? – Todd Jul 11 '18 at 03:37
  • Also, I see there's an assumption that both scripts in the pipeline are running in parallel. Is this assumption true - or do piped commands on linux execute sequentially (python test.py won't get the stdout of server.py until it completes). – Todd Jul 11 '18 at 03:45
  • @Todd If i don't pipe its, I can see the messages.May be the problem is the i use the pipe in wrong way. – FledgeXu Jul 11 '18 at 04:03
  • @SteffenUllrich Thanks it work! but why? – FledgeXu Jul 11 '18 at 04:06
  • Ah.. so the piped scripts do run parallel!? So @FledgeXu, turning off buffering means that the server script's output goes immediately to test.py. As opposed to buffered output which means server.py's output goes to fill up a buffer before the OS (or Python interp) decides to move the buffer contents to test.py. – Todd Jul 11 '18 at 04:11
  • @Todd It's should run in parallel, you can check out this [post](https://unix.stackexchange.com/questions/79501/executing-piped-commands-in-parallel). – FledgeXu Jul 11 '18 at 04:28
  • @FledgeXu: see my answer for an explanation why `-u` helps. – Steffen Ullrich Jul 11 '18 at 05:23

2 Answers2

3

By default the output from Python is buffered for performance reasons unless stdout goes to a terminal. This means any output from print will not be send immediately to the pipe but will first be buffered inside the Python process and only send if enough data have accumulated in the buffer. To switch this off and make any output be send immediately to the pipe use python -u or similar.
Note that this behavior is not unique to Python. Other commands (like perl) show a similar behavior.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
0

Assuming you don't want to turn off buffering completely (this could cause inefficiencies in other parts of the code), just call sys.stdout.flush() after each print statement in server.py.