Answers for "make a socket server side python"

0

make a socket server side python

import socket
import threading

HOST = '127.0.0.1'
PORT = 9090

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))

server.listen()

clients = []
nickname = []

def broadcast(message):
    for client in clients:
        client.send(message)

def handle(client, address):
    while True:
        try:
            message = client.recv(1024).decode('utf-8')
            print(f"Client {str(address)} says {message}")
            broadcast(message)
        except:
            clients.remove(client)
            print(f"Client {address} disconnected")
            client.close()
            pass

def receive():
    while True:
        client, address = server.accept()
        print(f"Connected with {str(address)}!")
        clients.append(client)
        broadcast(f"Client {address} connected to the server".encode('utf-8'))
        client.send("Connected to the server".encode('utf-8'))
        thread = threading.Thread(target=handle, args=(client, address))
        thread.start()

print("Server running...")

receive()
Posted by: Guest on August-24-2021
0

client server python socket

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection
Posted by: Guest on December-15-2020
-2

client server python socket

s = socket.socket (socket_family, socket_type, protocol=0)
Posted by: Guest on December-15-2020

Code answers related to "make a socket server side python"

Python Answers by Framework

Browse Popular Code Answers by Language