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 = []
defbroadcast(message):
for client in clients:
client.send(message)
defhandle(client, address):
whileTrue:
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
defreceive():
whileTrue:
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
1
socket python functions
#!/usr/bin/env python3import socket
HOST='127.0.0.1'# The server's hostname or IP addressPORT=65432# The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
#!/usr/bin/env python3import socket
HOST='127.0.0.1'# Standard loopback interface address (localhost)PORT=65432# Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
whileTrue:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
import socket
HOST='127.0.0.1'# Standard loopback interface address (localhost)PORT=65432# Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
whileTrue:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email