meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
programming:python:socket [2023/12/28 14:04] – created niziakprogramming:python:socket [2023/12/28 14:23] (current) niziak
Line 1: Line 1:
 ====== socket ====== ====== socket ======
  
 +[[https://docs.python.org/3/library/socket.html|socket — Low-level networking interface]]
 ===== socket.makefile ===== ===== socket.makefile =====
 +
 +[[https://docs.python.org/3/library/socket.html#socket.socket.makefile|socket.makefile]]
  
 When accessing socket as file: When accessing socket as file:
Line 7: Line 10:
   * read() reads until EOF, so EOF for sockets means that connection is closed. Workarounds:   * read() reads until EOF, so EOF for sockets means that connection is closed. Workarounds:
     * use ''\n'' at end of message and ''readline()'' for reading     * use ''\n'' at end of message and ''readline()'' for reading
-    * use own framing protocol. I.e. first 4 bytes is always size of data payload and first read exactly 4 bytes and then N bytes specified in header.+    * use own framing protocol. I.e. Encode 32bit size on first 4 bytes (using ''struct''). Then send data payload. On receiver side: read exactly 4 bytes and then N bytes specified in header. 
 +  * When file is in binary ''b'' mode, use ''str.encode()'' to convert strings into bytes 
 + 
 +