Been through the forum, docs and code looking for a solution or at least a hint about this, but wasn't quite successful. How do I get the host, port from a client connection? Dumbest echo application possible:
server
Code: Select all
from usocket import AF_INET, getaddrinfo, inet_ntop, socket, SOCK_STREAM
info = getaddrinfo("127.0.0.1", 65432, 0, SOCK_STREAM)
addr = info[0][-1]
print('Server Addr ', inet_ntop(AF_INET, addr))
s = socket()
s.bind(addr)
s.listen(0)
c, addr = s.accept()
print('Client Addr ', inet_ntop(AF_INET, addr))
while True:
data = c.recv(1024)
if not data:
break
c.send(data)
c.close()
s.close()
Code: Select all
from usocket import AF_INET, getaddrinfo, inet_ntop, socket, SOCK_STREAM
addr = getaddrinfo("127.0.0.1", 65432, 0, SOCK_STREAM)[0][-1]
print('Addr ', addr)
s = socket()
s.connect(addr)
s.send(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()