-
Notifications
You must be signed in to change notification settings - Fork 2
/
aio.sockets.html
152 lines (116 loc) · 4.2 KB
/
aio.sockets.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<html><head><meta charset="utf-8"></head><script src="https://pygame-web.github.io/pygbag/0.0/pythons.js" data-python="cpython3.13" type=module id="__main__" data-os="vtx,fs,gui" async defer>#<!--
import aio
import asyncio
import time
import socket
import select
# https://github.com/emscripten-core/emscripten/issues/4586 recv MSG_PEEK flag ignored
print(" *socktest* ")
import io, socket
import aio
async def aio_sock_open(sock, host, port):
while True:
try:
sock.connect(
(
host,
port,
)
)
except BlockingIOError:
await aio.sleep(0)
except OSError as e:
# 30 emsdk, 106 linux
if e.errno in (30, 106):
return sock
sys.print_exception(e)
class aio_sock:
def __init__(self, url, mode, tmout):
host, port = url.rsplit(":", 1)
self.port = int(port)
# we don't want simulator to fool us
if __WASM__ and __import__("platform").is_browser:
if not url.startswith("://"):
pdb(f"switching to {self.port}+20000 as websocket")
self.port += 20000
else:
_, host = host.split("://", 1)
self.host = host
self.sock = socket.socket()
# self.sock.setblocking(0)
# overload socket directly ?
def fileno(self):
return self.sock.fileno()
def send(self, *argv, **kw):
self.sock.send(*argv, **kw)
def recv(self, *argv):
return self.sock.recv(*argv)
# ============== specific ===========================
async def __aenter__(self):
# use async
print("64: aio_sock_open", self.host, self.port)
await aio_sock_open(self.sock, self.host, self.port)
return self
async def __aexit__(self, exc_type, exc, tb):
aio.protect.append(self)
aio.defer(self.sock.close, (), {})
del self.port, self.host, self.sock
def read(self, size=-1):
return self.recv(0)
def write(self, data):
if isinstance(data, str):
return self.sock.send(data.encode())
return self.sock.send(data)
def print(self, *argv, **kw):
kw["file"] = io.StringIO(newline="\r\n")
print(*argv, **kw)
self.write(kw["file"].getvalue())
def __enter__(url, mode, tmout):
# use softrt (wapy)
return aio.await_for(self.__aenter__())
def __exit__(self, exc_type, exc, tb):
# self.sock.close()
pass
async def irc_handler(sock):
buf = []
while not aio.exit:
rr, rw, re = select.select([sock.sock], [], [], 0)
if rr or rw or re:
while not aio.exit:
try:
# emscripten does not honor PEEK
# peek = sock.sock.recv(1, socket.MSG_PEEK |socket.MSG_DONTWAIT)
peek = sock.sock.recv(1, socket.MSG_DONTWAIT)
if peek:
buf.append(peek)
if peek == b"\n":
print("PEEK", b"".join(buf))
buf.clear()
break
else:
# lost con.
print("HANGUP", buf)
return
except BlockingIOError as e:
if e.errno == 6:
await aio.sleep(0)
else:
await aio.sleep(0)
sock.print("QUIT")
async def main():
global sock
print(" ------------- Hello WasmPython + sockets ----------")
window.MM.set_socket("wss://")
nick = "pygamer_" + str(time.time())[-5:].replace(".", "")
d = {"nick": nick, "channel": "#sav"}
# async with aio.open("wss://pmp-p.ddns.net/wss/6667:443", "a+", 5) as sock:
async with aio_sock("://pmp-p.ddns.net/wss/6667:443", "a+", 5) as sock:
sock.print("""CAP LS\r\nNICK {nick}\r\nUSER {nick} {nick} localhost :wsocket\r\nJOIN {channel}""".format(**d))
sock.print("PRIVMSG #sav :Salut à vous !")
await irc_handler(sock)
sys.exit(0)
async def custom_site():
await main()
asyncio.run(custom_site())
del custom_site
# --></script></html>