I try to make simple socket server but it just doesn't work. Client doesn't connect to server. And finally I get 10060 WinError. I tried pure socket implementation, I tried websockets implementation.
Pure socket [SERVER]:
import socket
HOST = 'XXX.XXX.XX.XX'
PORT = 65000
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)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Puse socket [CLIENT]:
import socket
HOST = 'XXX.XXX.XX.XX'
PORT = 65000
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))
WebSockets [SERVER]:
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print("<",name)
greeting = "Hello " + name + "!"
await websocket.send(greeting)
print(">", greeting)
start_server = websockets.serve(hello, "XXX.XXX.XX.XX", 18765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
WebSockets [CLIENT]:
import asyncio
import websockets
async def hello():
uri = "ws://XXX.XXX.XX.XX:18765"
async with websockets.connect(uri) as websocket:
name = input("What's your name? ")
await websocket.send(name)
print(f">", name)
greeting = await websocket.recv()
print(f"<", greeting)
asyncio.get_event_loop().run_until_complete(hello())
VPS OS - Debian 9, if it matters :/
Aucun commentaire:
Enregistrer un commentaire