python socket disconnect

import select
import socket

ip = '127.0.0.1'
port = 80

conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((ip, port))

while True:
    try:
        ready_to_read, ready_to_write, in_error = \
            select.select([conn,], [conn,], [], 5)
    except select.error:
        conn.shutdown(2)    # 0 = done receiving, 1 = done sending, 2 = both
        conn.close()
        # connection error event here, maybe reconnect
        print('connection error')
        break
    if len(ready_to_read) > 0:
        recv = conn.recv(2048)
        # do stuff with received data
        print(f'received: {recv}')
    if len(ready_to_write) > 0:
        # connection established, send some stuff
        conn.send('some stuff')

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
disconnect socket from client python python socket how to close a socket python socket error when client disconnects event on disconnect in sockets python python tcp socket detect disconnect python socket check if client disconnect disconnect room python-socketio python socketio disconnect connect disconnect python-socketio socket give response python python socket proper disconnect python-socketio disconnect client python socket socket time out connection socket.socket python close connection python socket io disconnect handler python socketio if disconnect client python socketio disconnect python socketio on_disconnect python socket connection close socket quit python server.disconnect python socket python socket terminate connection client socket python disconnect disconnect from socket client python disconnect socket python client propperly disconnect socket pyhton stop server socket python python socket server client disconnect python socketio disconnect() python socket how to close connection python connecting to socket python python socket respond disconnect socket python python socket handle client disconnect python socket on server python socket close connection disconnect the socket after first result in python socket stop connection client pythpn handle disconnect socket python disconnect from specific user socket python handle disconnect python socket python socket close client connection socketio disconnect py python close socket connection python socket.socket client close should the client close the socket python how to disconnect from socket server in python socket handle python get socket handle python python socket server crash client disconnect how to make a socket to listen again after the cilent disconncted python python socketio disconnect reason how to get reason of disconnect socketio python python disconnect socket server socket info python socket handle disconnect python client disconnect error socket python disconnect socket from server python socket disconnect python socketi o python connnection to server python socket disconnect client socket close python stop client socket python disconnect in socket python python socket detect disconnect python socket disconnect
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source