How to setup Python socket server correctly
I recently started learning Python but I came across a question. Why does my while True loop stops when I do socket.accept()
My code does that keeps printing 'HEY!!':
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
'''
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
'''
My code that only prints 'HEY!!' once:
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
And how can I solve that it keeps printing 'HEY!!' but also let the socket work?
Thanks for reading!
UPDATE:
It is working now, I am using threading to achieve it.
Do you have the same problem? -> Google: "Multiple while true loops threading python"
Thanks for everyone that helped me!
python python-3.x sockets
add a comment |
I recently started learning Python but I came across a question. Why does my while True loop stops when I do socket.accept()
My code does that keeps printing 'HEY!!':
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
'''
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
'''
My code that only prints 'HEY!!' once:
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
And how can I solve that it keeps printing 'HEY!!' but also let the socket work?
Thanks for reading!
UPDATE:
It is working now, I am using threading to achieve it.
Do you have the same problem? -> Google: "Multiple while true loops threading python"
Thanks for everyone that helped me!
python python-3.x sockets
add a comment |
I recently started learning Python but I came across a question. Why does my while True loop stops when I do socket.accept()
My code does that keeps printing 'HEY!!':
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
'''
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
'''
My code that only prints 'HEY!!' once:
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
And how can I solve that it keeps printing 'HEY!!' but also let the socket work?
Thanks for reading!
UPDATE:
It is working now, I am using threading to achieve it.
Do you have the same problem? -> Google: "Multiple while true loops threading python"
Thanks for everyone that helped me!
python python-3.x sockets
I recently started learning Python but I came across a question. Why does my while True loop stops when I do socket.accept()
My code does that keeps printing 'HEY!!':
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
'''
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
'''
My code that only prints 'HEY!!' once:
import socket
host = "0.0.0.0" #<- Not the real port and ip, I have working ones...
port = 1234
s = socket.socket()
s.bind((host, port))
s.listen(5)
while True:
print("HEY!!")
connection, adress = s.accept()
print("Got connection from: '" + str(adress[0]) + ":" + str(adress[1]) + "'")
And how can I solve that it keeps printing 'HEY!!' but also let the socket work?
Thanks for reading!
UPDATE:
It is working now, I am using threading to achieve it.
Do you have the same problem? -> Google: "Multiple while true loops threading python"
Thanks for everyone that helped me!
python python-3.x sockets
python python-3.x sockets
edited Nov 28 '18 at 14:56
Banjer_HD
asked Nov 14 '18 at 17:53
Banjer_HDBanjer_HD
597
597
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Why does my while True loop stops when I do socket.accept()
accept is a blocking operation. It waits until a client connects. It continues after the client has been connected and returns the socket for the new client connection.
My code that only prints 'HEY!!' once:
It will print HEY!! more than once if clients connect to your server and thus the blocking accept returns.
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
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%2f53306149%2fhow-to-setup-python-socket-server-correctly%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
Why does my while True loop stops when I do socket.accept()
accept is a blocking operation. It waits until a client connects. It continues after the client has been connected and returns the socket for the new client connection.
My code that only prints 'HEY!!' once:
It will print HEY!! more than once if clients connect to your server and thus the blocking accept returns.
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
add a comment |
Why does my while True loop stops when I do socket.accept()
accept is a blocking operation. It waits until a client connects. It continues after the client has been connected and returns the socket for the new client connection.
My code that only prints 'HEY!!' once:
It will print HEY!! more than once if clients connect to your server and thus the blocking accept returns.
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
add a comment |
Why does my while True loop stops when I do socket.accept()
accept is a blocking operation. It waits until a client connects. It continues after the client has been connected and returns the socket for the new client connection.
My code that only prints 'HEY!!' once:
It will print HEY!! more than once if clients connect to your server and thus the blocking accept returns.
Why does my while True loop stops when I do socket.accept()
accept is a blocking operation. It waits until a client connects. It continues after the client has been connected and returns the socket for the new client connection.
My code that only prints 'HEY!!' once:
It will print HEY!! more than once if clients connect to your server and thus the blocking accept returns.
answered Nov 14 '18 at 18:06
Steffen UllrichSteffen Ullrich
62.1k359101
62.1k359101
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
add a comment |
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
Thanks so much for your answer! So this means that 'HEY' will be printed after there is a socket connected and then it all starts again with waiting for a connection. Is it possible to have 2 different while loops? 1 that checks if a socket is connected and 1 to print out hey? Thanks again.
– Banjer_HD
Nov 14 '18 at 18:12
1
1
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
@Banjer_HD: One can use threads to have multiple while loops in the same program. But this is a different question.
– Steffen Ullrich
Nov 14 '18 at 18:22
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%2f53306149%2fhow-to-setup-python-socket-server-correctly%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