socket io python
# for installing socket io on PC
pip install python-socketio
socket io python
# for installing socket io on PC
pip install python-socketio
python socketio
import socketio
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
async def index(request):
"""Serve the client-side application."""
with open('index.html') as f:
return web.Response(text=f.read(), content_type='text/html')
@sio.event
def connect(sid, environ):
print("connect ", sid)
@sio.event
async def chat_message(sid, data):
print("message ", data)
await sio.emit('reply', room=sid)
@sio.event
def disconnect(sid):
print('disconnect ', sid)
app.router.add_static('/static', 'static')
app.router.add_get('/', index)
if __name__ == '__main__':
web.run_app(app)
python socket
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)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
python socket client
import socket
import threading
HOST = '127.0.0.1'
PORT = 9090
class Client:
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
receive_thread = threading.Thread(target=self.receive)
receive_thread.start()
def write(self, message):
message = message.encode('utf-8')
self.sock.send(message)
def stop(self):
self.sock.close()
def receive(self):
while True:
try:
message = self.sock.recv(1024).decode('utf-8')
print(message)
except ConnectionAbortedError:
break
except:
print("Error")
self.socket.close()
client = Client(HOST, PORT)
Copyright © 2021 Codeinu
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