-
-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[proposed user happiness]A little consideration for add some useful apis in SocketStream #544
Comments
Thanks for trying out Trio, and for sharing feedback! We definitely should have a high-level API for For In the mean time, here's a simple way of parsing lines out of a Trio stream: # The two magic values here are pretty arbitrary
async def lines_in_stream(stream, receive_chunk_size=16384, max_line_length=4096, separator=b"\r\n"):
buf = bytearray()
while True:
idx = buf.find(separator)
if idx == -1:
# Having a maximum line length is important to avoid DoS attacks
# And also reduces the impact of our naive O(n**2) line-ending finding algorithm
# (by putting a maximum limit on 'n')
if len(buf) >= max_line_length:
raise ProtocolError("line too long")
chunk = await stream.receive_some(read_chunk_size)
if not chunk:
# No more data
if buf:
raise ProtocolError("connection closed in the middle of a line")
else:
return
else:
yield buf[:idx]
del buf[:idx + len(separator)] This is an async generator, so you use it like: async for line in lines_in_stream(stream):
print("got line!", line) This uses the native async generator syntax that was introduced in Python 3.6. If you care about 3.5 support, then the async_generator library should let you backport it to 3.5. Also, I haven't actually tested this, I just typed it straight into the github comment box, but I think the basic idea is correct at least. Sorry for the bother – we're still working on sorting a lot of this stuff out :-). But hopefully that will let you get started for now, and it's great to hear which things are causing trouble for you; it helps us prioritize new features. |
Hi,
I'm trying to build up a TCP server project, and after a little exploring on
trio
, I think maybe we can add some apis in theSocketStream
class.Consider for the basic echo server :)
If we get a connection, I wonder how can we get client address(I think it's a common case for server to get client address)?
After a little hack into
trio
, I don't find out a way to get client address directly (except using the internal socket to get address likeserver_stream.socket.getpeeraddress()
, maybe I'm wrong..)I think exposing the api like
getpeeraddress
(of cause we can rename it likeget_remote_addr
), and recv_line(for some tcp based protocol which is using text, and they are separated by \r\n) can maketrio
more friendly.The implementation for get client address can be implement like this:
provide the
api
like recv_line maybe harmful...I need more investigation on it :(I'm sorry if I have make something wrong, please correct me :) What do you think about this ?
The text was updated successfully, but these errors were encountered: