Detect why a socket closed: FIN vs RST
Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?
The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?
import asyncio
import socket
async def async_main(loop):
server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)
loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)
while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))
python python-3.x sockets tcp python-asyncio
add a comment |
Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?
The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?
import asyncio
import socket
async def async_main(loop):
server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)
loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)
while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))
python python-3.x sockets tcp python-asyncio
2
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30
add a comment |
Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?
The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?
import asyncio
import socket
async def async_main(loop):
server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)
loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)
while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))
python python-3.x sockets tcp python-asyncio
Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?
The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?
import asyncio
import socket
async def async_main(loop):
server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)
loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)
while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))
python python-3.x sockets tcp python-asyncio
python python-3.x sockets tcp python-asyncio
asked Nov 16 '18 at 20:56
Michal CharemzaMichal Charemza
20k972106
20k972106
2
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30
add a comment |
2
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30
2
2
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30
add a comment |
1 Answer
1
active
oldest
votes
Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:
while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8: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%2f53345265%2fdetect-why-a-socket-closed-fin-vs-rst%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
Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:
while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8:22
add a comment |
Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:
while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8:22
add a comment |
Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:
while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)
Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:
while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)
edited Nov 17 '18 at 8:27
answered Nov 17 '18 at 7:55
user4815162342user4815162342
61.8k592145
61.8k592145
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8:22
add a comment |
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8:22
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8:22
Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.
– Michal Charemza
Nov 17 '18 at 8: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%2f53345265%2fdetect-why-a-socket-closed-fin-vs-rst%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
2
I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".
– James K Polk
Nov 17 '18 at 4:30