Python socket.accept() blocking code before call?Calling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)
How is Arya still alive?
Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?
Probability of taking balls without replacement from a bag question
Narcissistic cube asks who are we?
I might have messed up in the 'Future Work' section of my thesis
Has there been evidence of any other gods?
Identity of a supposed anonymous referee revealed through "Description" of the report
Can a planet still function with a damaged moon?
Ugin's Conjurant vs. un-preventable damage
What replaces x86 intrinsics for C when Apple ditches Intel CPUs for their own chips?
Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1
Which spells are in some way related to shadows or the Shadowfell?
Is it a good idea to copy a trader when investing?
JSON DeSerialization Help?
Add Columns to .csv from Multiple Files
Was Mohammed the most popular first name for boys born in Berlin in 2018?
How to avoid making self and former employee look bad when reporting on fixing former employee's work?
Best species to breed to intelligence
How likely are Coriolis-effect-based quirks to develop in starship crew members?
Is there an application which does HTTP PUT?
Why use steam instead of just hot air?
Why did Missandei say this?
How do carbureted and fuel injected engines compare in high altitude?
Names of the Six Tastes
Python socket.accept() blocking code before call?
Calling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to learn Python sockets and have hit a snare with the socket.accept() method. As I understand the method, once I call accept, the thread will sit and wait for an incoming connection (blocking all following code). However, in the code below, which I got from https://docs.python.org/2/library/socket.html and am using localhost. I added a print('hello') to the first line of the server. Yet the print doesn't appear until after I disconnect the client. Why is this? Why does accept seem to run before my print yet after I bind the socket?
# Echo server program
import socket
print('hello') # This doesn't print until I disconnect the client
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
python sockets blocking
|
show 1 more comment
I am trying to learn Python sockets and have hit a snare with the socket.accept() method. As I understand the method, once I call accept, the thread will sit and wait for an incoming connection (blocking all following code). However, in the code below, which I got from https://docs.python.org/2/library/socket.html and am using localhost. I added a print('hello') to the first line of the server. Yet the print doesn't appear until after I disconnect the client. Why is this? Why does accept seem to run before my print yet after I bind the socket?
# Echo server program
import socket
print('hello') # This doesn't print until I disconnect the client
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
python sockets blocking
1
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try addingsys.stdout.flush()
after theprint
(remembering to alsoimport sys
before that).
– user4815162342
Mar 23 at 8:57
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
1
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (whichprint
inserts automatically).
– user4815162342
Mar 23 at 9:26
1
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to runpython -u
, which should force unbuffered standard streams.
– user4815162342
Mar 23 at 9:27
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39
|
show 1 more comment
I am trying to learn Python sockets and have hit a snare with the socket.accept() method. As I understand the method, once I call accept, the thread will sit and wait for an incoming connection (blocking all following code). However, in the code below, which I got from https://docs.python.org/2/library/socket.html and am using localhost. I added a print('hello') to the first line of the server. Yet the print doesn't appear until after I disconnect the client. Why is this? Why does accept seem to run before my print yet after I bind the socket?
# Echo server program
import socket
print('hello') # This doesn't print until I disconnect the client
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
python sockets blocking
I am trying to learn Python sockets and have hit a snare with the socket.accept() method. As I understand the method, once I call accept, the thread will sit and wait for an incoming connection (blocking all following code). However, in the code below, which I got from https://docs.python.org/2/library/socket.html and am using localhost. I added a print('hello') to the first line of the server. Yet the print doesn't appear until after I disconnect the client. Why is this? Why does accept seem to run before my print yet after I bind the socket?
# Echo server program
import socket
print('hello') # This doesn't print until I disconnect the client
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
python sockets blocking
python sockets blocking
asked Mar 23 at 8:49
tiggybitstiggybits
375
375
1
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try addingsys.stdout.flush()
after theprint
(remembering to alsoimport sys
before that).
– user4815162342
Mar 23 at 8:57
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
1
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (whichprint
inserts automatically).
– user4815162342
Mar 23 at 9:26
1
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to runpython -u
, which should force unbuffered standard streams.
– user4815162342
Mar 23 at 9:27
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39
|
show 1 more comment
1
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try addingsys.stdout.flush()
after theprint
(remembering to alsoimport sys
before that).
– user4815162342
Mar 23 at 8:57
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
1
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (whichprint
inserts automatically).
– user4815162342
Mar 23 at 9:26
1
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to runpython -u
, which should force unbuffered standard streams.
– user4815162342
Mar 23 at 9:27
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39
1
1
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try adding
sys.stdout.flush()
after the print
(remembering to also import sys
before that).– user4815162342
Mar 23 at 8:57
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try adding
sys.stdout.flush()
after the print
(remembering to also import sys
before that).– user4815162342
Mar 23 at 8:57
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
1
1
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (which
print
inserts automatically).– user4815162342
Mar 23 at 9:26
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (which
print
inserts automatically).– user4815162342
Mar 23 at 9:26
1
1
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run
python -u
, which should force unbuffered standard streams.– user4815162342
Mar 23 at 9:27
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run
python -u
, which should force unbuffered standard streams.– user4815162342
Mar 23 at 9:27
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39
|
show 1 more comment
1 Answer
1
active
oldest
votes
You are likely using an output device on a system that Python's IO doesn't recognize as interactive. As a workaround, you can add sys.stdout.flush()
after the print
.
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill the whole buffer (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal service, the IO layer automatically switches to line buffering, where the buffer is also flushed whenever a newline character is printed (and which the print
statement inserts automatically).
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run python -u
, which should force unbuffered standard streams.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312104%2fpython-socket-accept-blocking-code-before-call%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are likely using an output device on a system that Python's IO doesn't recognize as interactive. As a workaround, you can add sys.stdout.flush()
after the print
.
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill the whole buffer (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal service, the IO layer automatically switches to line buffering, where the buffer is also flushed whenever a newline character is printed (and which the print
statement inserts automatically).
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run python -u
, which should force unbuffered standard streams.
add a comment |
You are likely using an output device on a system that Python's IO doesn't recognize as interactive. As a workaround, you can add sys.stdout.flush()
after the print
.
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill the whole buffer (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal service, the IO layer automatically switches to line buffering, where the buffer is also flushed whenever a newline character is printed (and which the print
statement inserts automatically).
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run python -u
, which should force unbuffered standard streams.
add a comment |
You are likely using an output device on a system that Python's IO doesn't recognize as interactive. As a workaround, you can add sys.stdout.flush()
after the print
.
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill the whole buffer (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal service, the IO layer automatically switches to line buffering, where the buffer is also flushed whenever a newline character is printed (and which the print
statement inserts automatically).
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run python -u
, which should force unbuffered standard streams.
You are likely using an output device on a system that Python's IO doesn't recognize as interactive. As a workaround, you can add sys.stdout.flush()
after the print
.
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill the whole buffer (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal service, the IO layer automatically switches to line buffering, where the buffer is also flushed whenever a newline character is printed (and which the print
statement inserts automatically).
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run python -u
, which should force unbuffered standard streams.
answered Mar 23 at 11:50
user4815162342user4815162342
65.8k699156
65.8k699156
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312104%2fpython-socket-accept-blocking-code-before-call%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How are you running the server, on the command line, or inside an interactive environment that might try to capture its output? Try adding
sys.stdout.flush()
after theprint
(remembering to alsoimport sys
before that).– user4815162342
Mar 23 at 8:57
@user4815162342 I am running in the command line. Yet when I used flush it worked! Could you explain in detail? Thanks!
– tiggybits
Mar 23 at 9:14
1
The standard output is a buffered stream, meaning that when you print something, the output sticks around in an internal buffer until you print enough data to fill it (unlikely in a small program, the buffet is several kilobytes in size), or until the program exits, when all such buffers are automatically flushed. Normally when the output is a terminal, the IO layer automatically switches to line buffering, where the buffer is flushed whenever a newline character is printed (which
print
inserts automatically).– user4815162342
Mar 23 at 9:26
1
For some reason, that doesn't work on your system, and you have to flush explicitly. Another option is to run
python -u
, which should force unbuffered standard streams.– user4815162342
Mar 23 at 9:27
Ok! So I was using git bash for windows and that seems to have been the issue. I changed over to my linux terminal and now everything is working as expected! Thanks for the help :)
– tiggybits
Mar 23 at 9:39