Answers for "socket connection\exception python"

1

connection refused socket python

port = 12397 # Reserve a port for your service
s.bind(('', port)) #Bind to the port
Posted by: Guest on March-02-2021
1

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')
Posted by: Guest on April-08-2021

Code answers related to "socket connection\exception python"

Python Answers by Framework

Browse Popular Code Answers by Language