@@ -12,7 +12,7 @@ msgid ""
12
12
msgstr ""
13
13
"Project-Id-Version : Python 3.13\n "
14
14
"Report-Msgid-Bugs-To : \n "
15
- "POT-Creation-Date : 2025-01-31 14:16 +0000\n "
15
+ "POT-Creation-Date : 2025-02-07 14:17 +0000\n "
16
16
"PO-Revision-Date : 2021-06-28 01:13+0000\n "
17
17
"Last-Translator : Stan Ulbrych, 2025\n "
18
18
"Language-Team : Polish (https://app.transifex.com/python-doc/teams/5390/pl/)\n "
@@ -495,11 +495,17 @@ msgid ""
495
495
"\n"
496
496
" def handle(self):\n"
497
497
" # self.request is the TCP socket connected to the client\n"
498
- " self.data = self.request.recv(1024).strip()\n"
499
- " print(\" Received from {}:\" .format(self.client_address[0]))\n"
500
- " print(self.data)\n"
498
+ " pieces = [b'']\n"
499
+ " total = 0\n"
500
+ " while b'\\ n' not in pieces[-1] and total < 10_000:\n"
501
+ " pieces.append(self.request.recv(2000))\n"
502
+ " total += len(pieces[-1])\n"
503
+ " self.data = b''.join(pieces)\n"
504
+ " print(f\" Received from {self.client_address[0]}:\" )\n"
505
+ " print(self.data.decode(\" utf-8\" ))\n"
501
506
" # just send back the same data, but upper-cased\n"
502
507
" self.request.sendall(self.data.upper())\n"
508
+ " # after we return, the socket will be closed.\n"
503
509
"\n"
504
510
"if __name__ == \" __main__\" :\n"
505
511
" HOST, PORT = \" localhost\" , 9999\n"
@@ -521,11 +527,12 @@ msgid ""
521
527
"class MyTCPHandler(socketserver.StreamRequestHandler):\n"
522
528
"\n"
523
529
" def handle(self):\n"
524
- " # self.rfile is a file-like object created by the handler;\n"
525
- " # we can now use e.g. readline() instead of raw recv() calls\n"
526
- " self.data = self.rfile.readline().strip()\n"
527
- " print(\" {} wrote:\" .format(self.client_address[0]))\n"
528
- " print(self.data)\n"
530
+ " # self.rfile is a file-like object created by the handler.\n"
531
+ " # We can now use e.g. readline() instead of raw recv() calls.\n"
532
+ " # We limit ourselves to 10000 bytes to avoid abuse by the sender.\n"
533
+ " self.data = self.rfile.readline(10000).rstrip()\n"
534
+ " print(f\" {self.client_address[0]} wrote:\" )\n"
535
+ " print(self.data.decode(\" utf-8\" ))\n"
529
536
" # Likewise, self.wfile is a file-like object used to write back\n"
530
537
" # to the client\n"
531
538
" self.wfile.write(self.data.upper())"
@@ -534,9 +541,12 @@ msgstr ""
534
541
msgid ""
535
542
"The difference is that the ``readline()`` call in the second handler will "
536
543
"call ``recv()`` multiple times until it encounters a newline character, "
537
- "while the single ``recv()`` call in the first handler will just return what "
538
- "has been received so far from the client's ``sendall()`` call (typically all "
539
- "of it, but this is not guaranteed by the TCP protocol)."
544
+ "while the the first handler had to use a ``recv()`` loop to accumulate data "
545
+ "until a newline itself. If it had just used a single ``recv()`` without the "
546
+ "loop it would just have returned what has been received so far from the "
547
+ "client. TCP is stream based: data arrives in the order it was sent, but "
548
+ "there no correlation between client ``send()`` or ``sendall()`` calls and "
549
+ "the number of ``recv()`` calls on the server required to receive it."
540
550
msgstr ""
541
551
542
552
msgid "This is the client side::"
@@ -553,13 +563,14 @@ msgid ""
553
563
"with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n"
554
564
" # Connect to server and send data\n"
555
565
" sock.connect((HOST, PORT))\n"
556
- " sock.sendall(bytes(data + \"\\ n\" , \" utf-8\" ))\n"
566
+ " sock.sendall(bytes(data, \" utf-8\" ))\n"
567
+ " sock.sendall(b\"\\ n\" )\n"
557
568
"\n"
558
569
" # Receive data from the server and shut down\n"
559
570
" received = str(sock.recv(1024), \" utf-8\" )\n"
560
571
"\n"
561
- "print(\" Sent: {} \" .format( data) )\n"
562
- "print(\" Received: {} \" .format( received) )"
572
+ "print(\" Sent: \" , data)\n"
573
+ "print(\" Received:\" , received)"
563
574
msgstr ""
564
575
565
576
msgid "The output of the example should look something like this:"
@@ -605,7 +616,7 @@ msgid ""
605
616
" def handle(self):\n"
606
617
" data = self.request[0].strip()\n"
607
618
" socket = self.request[1]\n"
608
- " print(\" {} wrote: \" .format( self.client_address[0]) )\n"
619
+ " print(f \" {self.client_address[0]} wrote: \" )\n"
609
620
" print(data)\n"
610
621
" socket.sendto(data.upper(), self.client_address)\n"
611
622
"\n"
@@ -630,8 +641,8 @@ msgid ""
630
641
"sock.sendto(bytes(data + \"\\ n\" , \" utf-8\" ), (HOST, PORT))\n"
631
642
"received = str(sock.recv(1024), \" utf-8\" )\n"
632
643
"\n"
633
- "print(\" Sent: {} \" .format( data) )\n"
634
- "print(\" Received: {} \" .format( received) )"
644
+ "print(\" Sent: \" , data)\n"
645
+ "print(\" Received:\" , received)"
635
646
msgstr ""
636
647
637
648
msgid ""
0 commit comments