[Tutor] creating a server
dn
PyTutor at DancesWithMice.info
Wed Oct 7 15:09:32 EDT 2020
On 08/10/2020 07:29, Alan Gauld via Tutor wrote:
> On 07/10/2020 19:13, nathan tech wrote:
>> I certainly have and was going to base my code around it but was unsure
>> if sockets could handle the passing of objects?
>
> sockets only care about bytes, they know nothing about data types.
> That's up to you! You will need to choose a format to encode
> your data in the message - JSON or YAML or XML or ASN.1 etc.
> Then use a parser to pack/unpack the high level into and out
> of the data stream.
>
> But your own suggested server was still using sockets and reading bytes.
>
> You had a recv(1024) call. That reads 1024 bytes. What would happen
> if your "object" was 1500 bytes long? You'd only have part of it to process.
One of the oldest and most popular (client-)servers on the Internet is
Apache's httpd. It implements the httpd protocol (yes, well...). The
client first sends a "header", and later a "body" payload. The header
'declares' the business of the body, including its length - per @Alan's
comment.
The early web and email servers used plain-text as their universal
communication medium (and very handy for debugging). In some respects
this can be inefficient, eg transmission volume, security. Many 'modern'
approaches try for a more 'binary' approach.
Recommendation: design the protocol(s), ie the 'conversation' between
client and server, before worrying about the code (which will implement
same). Your server, your rules! (but what are "the rules"?)
The code specified (earlier) is a classic "Event Loop". These days,
looking-up such key-words will likely land you in the world of
computer-games, but the techniques as you (originally) noted, still apply.
Adding to the study/reading @Alan recommended:
- RFCs or similar, for Internet infrastructure servers.
- YAML, JSON, etc, file formats and processing libraries.
- more 'modern' protocols, eg MongoDB.
- Python3's (default) use of Unicode characters, cf bytes, ASCII
characters, etc.
--
Regards =dn
More information about the Tutor
mailing list