-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsockets.po
More file actions
326 lines (263 loc) · 50.9 KB
/
Copy pathsockets.po
File metadata and controls
326 lines (263 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# Danial Behzadi <dani.behzi@ubuntu.com>, 2025
# Rafael Fontenelle <rffontenelle@gmail.com>, 2025
# Revisto <theRevisto@gmail.com>, 2025
# Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026
#
msgid ""
msgstr ""
"Project-Id-Version: Python 3.14\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-09-16 12:56+0000\n"
"PO-Revision-Date: 2026-09-15 10:12+0330\n"
"Last-Translator: Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026\n"
"Language-Team: Persian (https://github.com/python/python-docs-fa/fa/)\n"
"Language: fa\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 3.6\n"
msgid "Socket Programming HOWTO"
msgstr "راهنمای عملی برنامهنویسی سوکت"
msgid "Author"
msgstr "نویسنده"
msgid "Gordon McMillan"
msgstr "گوردون مکمیلان"
msgid "Abstract"
msgstr "چکیده"
msgid "Sockets are used nearly everywhere, but are one of the most severely misunderstood technologies around. This is a 10,000 foot overview of sockets. It's not really a tutorial - you'll still have work to do in getting things operational. It doesn't cover the fine points (and there are a lot of them), but I hope it will give you enough background to begin using them decently."
msgstr "سوکتها تقریباً همهجا استفاده میشوند، اما یکی از فناوریهایی هستند که بیشترین سوءتفاهم دربارهی آنها وجود دارد. این یک نمای کلی از ارتفاع ۱۰٬۰۰۰ پایی از سوکتهاست. این واقعاً یک آموزش نیست؛ شما همچنان برای راهاندازی عملیاتی آنها کارهایی برای انجام خواهید داشت. این نمای کلی نکات ظریف را پوشش نمیدهد (و تعداد آنها زیاد است)، اما امیدوارم پیشزمینهی کافی برای شروع استفادهی مناسب از آنها به شما بدهد."
msgid "Sockets"
msgstr "سوکتها"
msgid "I'm only going to talk about INET (i.e. IPv4) sockets, but they account for at least 99% of the sockets in use. And I'll only talk about STREAM (i.e. TCP) sockets - unless you really know what you're doing (in which case this HOWTO isn't for you!), you'll get better behavior and performance from a STREAM socket than anything else. I will try to clear up the mystery of what a socket is, as well as some hints on how to work with blocking and non-blocking sockets. But I'll start by talking about blocking sockets. You'll need to know how they work before dealing with non-blocking sockets."
msgstr "من فقط قصد دارم دربارهی سوکتهای INET (یعنی IPv4) صحبت کنم، اما این سوکتها دستکم ۹۹٪ از سوکتهای مورد استفاده را تشکیل میدهند. و فقط دربارهی سوکتهای STREAM (یعنی TCP) صحبت خواهم کرد — مگر اینکه واقعاً بدانید چه کار میکنید (که در این صورت این راهنمای عملی بدرد شما نمیخورد!)، از یک سوکت STREAM رفتار و کارایی بهتری نسبت به هر چیز دیگری خواهید گرفت. سعی میکنم ابهام دربارهی چیستی سوکت را برطرف کنم، و همچنین نکاتی دربارهی نحوهی کار با سوکتهای مسدودکننده و غیرمسدودکننده ارائه دهم. اما صحبت را با سوکتهای مسدودکننده آغاز میکنم. پیش از کار با سوکتهای غیرمسدودکننده، باید بدانید که آنها چگونه کار میکنند."
msgid "Part of the trouble with understanding these things is that \"socket\" can mean a number of subtly different things, depending on context. So first, let's make a distinction between a \"client\" socket - an endpoint of a conversation, and a \"server\" socket, which is more like a switchboard operator. The client application (your browser, for example) uses \"client\" sockets exclusively; the web server it's talking to uses both \"server\" sockets and \"client\" sockets."
msgstr "بخشی از دشواری درک این موارد آن است که «سوکت» میتواند بسته به زمینه، به چند چیز با تفاوتهای ظریف اشاره داشته باشد. بنابراین ابتدا، بیایید میان یک سوکت «کلاینت»، یعنی پایانهی یک مکالمه، و یک سوکت «سرور» که بیشتر شبیه یک اپراتور تلفنخانه است، تمایز قائل شویم. برنامهی کلاینت (برای مثال، مرورگر شما) منحصراً از سوکتهای «کلاینت» استفاده میکند؛ سرور وبی که با آن گفتوگو میکند، هم از سوکتهای «سرور» و هم از سوکتهای «کلاینت» استفاده میکند."
msgid "History"
msgstr "تاریخچه"
msgid "Of the various forms of :abbr:`IPC (Inter Process Communication)`, sockets are by far the most popular. On any given platform, there are likely to be other forms of IPC that are faster, but for cross-platform communication, sockets are about the only game in town."
msgstr "از میان اشکال گوناگون :abbr:`IPC (Inter Process Communication)`، سوکتها با اختلاف محبوبترین هستند. در هر سکوی معین، احتمال دارد اشکال دیگری از IPC وجود داشته باشند که سریعتر باشند، اما برای ارتباط بینسکویی، سوکتها تقریباً تنها گزینه موجود هستند."
msgid "They were invented in Berkeley as part of the BSD flavor of Unix. They spread like wildfire with the internet. With good reason --- the combination of sockets with INET makes talking to arbitrary machines around the world unbelievably easy (at least compared to other schemes)."
msgstr "آنها در برکلی بهعنوان بخشی از نسخهی BSD از یونیکس ابداع شدند. با اینترنت، مانند آتش در علف خشک گسترش یافتند. بهدلیل موجهی --- ترکیب سوکتها با INET، ارتباط با هر ماشین دلخواهی در سراسر جهان را بهطرز باورنکردنی آسان میکند (حداقل در مقایسه با روشهای دیگر)."
msgid "Creating a Socket"
msgstr "ایجاد یک سوکت"
msgid "Roughly speaking, when you clicked on the link that brought you to this page, your browser did something like the following::"
msgstr "بهطور تقریبی، هنگامی که روی پیوندی که شما را به این صفحه آورد کلیک کردید، مرورگر شما کاری مشابه زیر انجام داد::"
msgid ""
"# create an INET, STREAMing socket\n"
"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n"
"# now connect to the web server on port 80 - the normal http port\n"
"s.connect((\"www.python.org\", 80))"
msgstr ""
"# create an INET, STREAMing socket\n"
"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n"
"# now connect to the web server on port 80 - the normal http port\n"
"s.connect((\"www.python.org\", 80))"
msgid "When the ``connect`` completes, the socket ``s`` can be used to send in a request for the text of the page. The same socket will read the reply, and then be destroyed. That's right, destroyed. Client sockets are normally only used for one exchange (or a small set of sequential exchanges)."
msgstr "هنگامی که ``connect`` کامل میشود، میتوان از سوکت ``s`` برای ارسال درخواستی برای متن صفحه استفاده کرد. همان سوکت پاسخ را میخواند و سپس نابود میشود. درست است، نابود میشود. سوکتهای کلاینت معمولاً تنها برای یک تبادل (یا مجموعه کوچکی از تبادلهای متوالی) استفاده میشوند."
msgid "What happens in the web server is a bit more complex. First, the web server creates a \"server socket\"::"
msgstr "آنچه در وبسرور رخ میدهد کمی پیچیدهتر است. ابتدا، وبسرور یک «سوکت سرور» ایجاد میکند::"
msgid ""
"# create an INET, STREAMing socket\n"
"serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n"
"# bind the socket to a public host, and a well-known port\n"
"serversocket.bind((socket.gethostname(), 80))\n"
"# become a server socket\n"
"serversocket.listen(5)"
msgstr ""
"# create an INET, STREAMing socket\n"
"serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n"
"# bind the socket to a public host, and a well-known port\n"
"serversocket.bind((socket.gethostname(), 80))\n"
"# become a server socket\n"
"serversocket.listen(5)"
msgid "A couple things to notice: we used ``socket.gethostname()`` so that the socket would be visible to the outside world. If we had used ``s.bind(('localhost', 80))`` or ``s.bind(('127.0.0.1', 80))`` we would still have a \"server\" socket, but one that was only visible within the same machine. ``s.bind(('', 80))`` specifies that the socket is reachable by any address the machine happens to have."
msgstr "چند نکته قابل توجه: ما از ``socket.gethostname()`` استفاده کردیم تا سوکت برای دنیای بیرون قابل مشاهده باشد. اگر از ``s.bind(('localhost', 80))`` یا ``s.bind(('127.0.0.1', 80))`` استفاده کرده بودیم، همچنان یک سوکت «سرور» داشتیم، اما سوکتی که فقط در همان ماشین قابل مشاهده بود. ``s.bind(('', 80))`` مشخص میکند که سوکت از طریق هر نشانی که ماشین ممکن است داشته باشد، قابل دسترسی است."
msgid "A second thing to note: low number ports are usually reserved for \"well known\" services (HTTP, SNMP etc). If you're playing around, use a nice high number (4 digits)."
msgstr "دومین نکته: پورتهای با شماره کم معمولاً برای سرویسهای «شناختهشده» (HTTP، SNMP و غیره) رزرو شدهاند. اگر در حال آزمایش هستید، از یک شماره بالا و مناسب (۴ رقمی) استفاده کنید."
msgid "Finally, the argument to ``listen`` tells the socket library that we want it to queue up as many as 5 connect requests (the normal max) before refusing outside connections. If the rest of the code is written properly, that should be plenty."
msgstr "در نهایت، آرگومان ``listen`` به کتابخانهی سوکت میگوید که ما میخواهیم تا ۵ درخواست اتصال (حداکثر معمول) را پیش از رد کردن اتصالهای بیرونی در صف قرار دهد. اگر بقیهی کد بهدرستی نوشته شده باشد، این مقدار باید کافی باشد."
msgid "Now that we have a \"server\" socket, listening on port 80, we can enter the mainloop of the web server::"
msgstr "اکنون که یک سوکت «سرور» داریم که به پورت ۸۰ گوش میدهد، میتوانیم وارد حلقهی اصلی وبسرور شویم::"
msgid ""
"while True:\n"
" # accept connections from outside\n"
" (clientsocket, address) = serversocket.accept()\n"
" # now do something with the clientsocket\n"
" # in this case, we'll pretend this is a threaded server\n"
" ct = make_client_thread(clientsocket)\n"
" ct.start()"
msgstr ""
"while True:\n"
" # accept connections from outside\n"
" (clientsocket, address) = serversocket.accept()\n"
" # now do something with the clientsocket\n"
" # in this case, we'll pretend this is a threaded server\n"
" ct = make_client_thread(clientsocket)\n"
" ct.start()"
msgid "There's actually 3 general ways in which this loop could work - dispatching a thread to handle ``clientsocket``, create a new process to handle ``clientsocket``, or restructure this app to use non-blocking sockets, and multiplex between our \"server\" socket and any active ``clientsocket``\\ s using ``select``. More about that later. The important thing to understand now is this: this is *all* a \"server\" socket does. It doesn't send any data. It doesn't receive any data. It just produces \"client\" sockets. Each ``clientsocket`` is created in response to some *other* \"client\" socket doing a ``connect()`` to the host and port we're bound to. As soon as we've created that ``clientsocket``, we go back to listening for more connections. The two \"clients\" are free to chat it up - they are using some dynamically allocated port which will be recycled when the conversation ends."
msgstr "در واقع این حلقه میتواند به ۳ روش کلی کار کند - راهاندازی یک نخ برای رسیدگی به ``clientsocket``، ایجاد یک فرایند جدید برای رسیدگی به ``clientsocket``، یا تغییر ساختار این برنامه برای استفاده از سوکتهای غیرمسدودکننده و مالتیپلکس کردن (multiplex) بین سوکت «سرور» خودمان و هر ``clientsocket`` فعالی با استفاده از ``select``. بعداً دربارهی آن بیشتر صحبت خواهیم کرد. نکته مهمی که اکنون باید درک کنید این است: این *تمام* کاری است که یک سوکت «سرور» انجام میدهد. هیچ دادهای ارسال نمیکند. هیچ دادهای دریافت نمیکند. فقط سوکتهای «کلاینت» تولید میکند. هر ``clientsocket`` در پاسخ به ``connect()`` کردن یک سوکت «کلاینت» *دیگر* به میزبان و پورتی که به آنها مقید شدهایم، ایجاد میشود. بهمحض ایجاد آن ``clientsocket``، دوباره به گوشدادن برای اتصالهای بیشتر بازمیگردیم. دو «کلاینت» آزادند با یکدیگر گفتگو کنند - آنها از پورتی که بهصورت پویا تخصیصیافته است استفاده میکنند، که پس از پایان گفتگو بازیافت میشود."
msgid "IPC"
msgstr "ارتباط بینفرایندی (IPC)"
msgid "If you need fast IPC between two processes on one machine, you should look into pipes or shared memory. If you do decide to use AF_INET sockets, bind the \"server\" socket to ``'localhost'``. On most platforms, this will take a shortcut around a couple of layers of network code and be quite a bit faster."
msgstr "اگر به ارتباط بینفرایندی (IPC) سریع بین دو پردازه روی یک ماشین نیاز دارید، باید پایپها یا حافظه مشترک را بررسی کنید. اگر تصمیم گرفتید از سوکتهای AF_INET استفاده کنید، سوکت «سرور» را به ``'localhost'`` اختصاص دهید. در بیشتر پلتفرمها، این کار با میانبر زدن چند لایه از کد شبکه، بهمراتب سریعتر خواهد بود."
msgid "The :mod:`multiprocessing` integrates cross-platform IPC into a higher-level API."
msgstr ":mod:`multiprocessing` ارتباط بینفرایندی بینسکویی (IPC) را در یک API سطح بالاتر ادغام میکند."
msgid "Using a Socket"
msgstr "استفاده از سوکت"
msgid "The first thing to note, is that the web browser's \"client\" socket and the web server's \"client\" socket are identical beasts. That is, this is a \"peer to peer\" conversation. Or to put it another way, *as the designer, you will have to decide what the rules of etiquette are for a conversation*. Normally, the ``connect``\\ ing socket starts the conversation, by sending in a request, or perhaps a signon. But that's a design decision - it's not a rule of sockets."
msgstr "نخستین نکتهای که باید به آن توجه کنید این است که سوکت «کلاینت» مرورگر وب و سوکت «کلاینت» وبسرور، هر دو کاملاً یکسان هستند. یعنی این یک گفتوگوی «همتا به همتا» است. یا به بیان دیگر، *بهعنوان طراح، شما باید تصمیم بگیرید که آداب گفتوگو چیست*. بهطور معمول، سوکتی که ``connect`` را فراخوانی میکند، گفتوگو را با ارسال یک درخواست، یا شاید یک ورود (signon)، آغاز میکند. اما این یک تصمیم طراحی است؛ قانون سوکتها نیست."
msgid "Now there are two sets of verbs to use for communication. You can use ``send`` and ``recv``, or you can transform your client socket into a file-like beast and use ``read`` and ``write``. The latter is the way Java presents its sockets. I'm not going to talk about it here, except to warn you that you need to use ``flush`` on sockets. These are buffered \"files\", and a common mistake is to ``write`` something, and then ``read`` for a reply. Without a ``flush`` in there, you may wait forever for the reply, because the request may still be in your output buffer."
msgstr "اکنون دو مجموعه از فعلها برای استفاده در ارتباط وجود دارد. شما میتوانید از ``send`` و ``recv`` استفاده کنید، یا میتوانید سوکت کلاینت خود را به چیزی شبیه پرونده تبدیل کنید و از ``read`` و ``write`` استفاده کنید. روش دوم شیوهای است که Java سوکتهای خود را ارائه میدهد. من قصد ندارم اینجا درباره آن صحبت کنم، مگر برای هشدار دادن به شما که باید روی سوکتها از ``flush`` استفاده کنید. اینها «پروندههایی» دارای بافر هستند، و یک اشتباه رایج این است که چیزی را ``write`` کنید و سپس برای دریافت پاسخ ``read`` کنید. بدون یک ``flush`` در این میان، ممکن است برای همیشه منتظر پاسخ بمانید، زیرا ممکن است درخواست هنوز در بافر خروجی شما باشد."
msgid "Now we come to the major stumbling block of sockets - ``send`` and ``recv`` operate on the network buffers. They do not necessarily handle all the bytes you hand them (or expect from them), because their major focus is handling the network buffers. In general, they return when the associated network buffers have been filled (``send``) or emptied (``recv``). They then tell you how many bytes they handled. It is *your* responsibility to call them again until your message has been completely dealt with."
msgstr "اکنون به مانع اصلی سوکتها میرسیم — ``send`` و ``recv`` بر روی بافرهای شبکه عمل میکنند. آنها لزوماً همهی بایتهایی را که به آنها میسپارید (یا از آنها انتظار دارید) پردازش نمیکنند، زیرا تمرکز اصلی آنها رسیدگی به بافرهای شبکه است. بهطور کلی، آنها زمانی برمیگردند که بافرهای شبکهی مرتبط پر شده (``send``) یا خالی شده (``recv``) باشند. سپس به شما میگویند که چند بایت را پردازش کردهاند. این وظیفهی *شما* است که آنها را دوباره فراخوانی کنید تا پیام شما بهطور کامل رسیدگی شود."
msgid "When a ``recv`` returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever. You may be able to send data successfully; I'll talk more about this later."
msgstr "وقتی ``recv`` ۰ بایت برمیگرداند، به این معنا است که طرف مقابل اتصال را بسته است (یا در حال بستن آن است). شما دیگر هیچ دادهای را بر روی این اتصال دریافت نخواهید کرد. هرگز. ممکن است بتوانید دادهها را با موفقیت ارسال کنید؛ بعداً بیشتر در این مورد صحبت خواهم کرد."
msgid "A protocol like HTTP uses a socket for only one transfer. The client sends a request, then reads a reply. That's it. The socket is discarded. This means that a client can detect the end of the reply by receiving 0 bytes."
msgstr "پروتکلی مانند HTTP از یک سوکت فقط برای یک انتقال استفاده میکند. کلاینت درخواستی ارسال میکند، سپس پاسخی را میخواند. همین کافی است. سوکت دور انداخته میشود. این بدان معناست که یک کلاینت میتواند با دریافت ۰ بایت، پایان پاسخ را تشخیص دهد."
msgid "But if you plan to reuse your socket for further transfers, you need to realize that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket ``send`` or ``recv`` returns after handling 0 bytes, the connection has been broken. If the connection has *not* been broken, you may wait on a ``recv`` forever, because the socket will *not* tell you that there's nothing more to read (for now). Now if you think about that a bit, you'll come to realize a fundamental truth of sockets: *messages must either be fixed length* (yuck), *or be delimited* (shrug), *or indicate how long they are* (much better), *or end by shutting down the connection*. The choice is entirely yours, (but some ways are righter than others)."
msgstr "اما اگر قصد دارید از سوکت خود برای انتقالهای بعدی دوباره استفاده کنید، باید بدانید که *هیچ* :abbr:`EOT (End of Transfer)` *روی یک سوکت وجود ندارد.* تکرار میکنم: اگر ``send`` یا ``recv`` یک سوکت پس از پردازش ۰ بایت بازگشت کند، اتصال قطع شده است. اگر اتصال قطع *نشده* باشد، ممکن است برای همیشه منتظر ``recv`` بمانید، زیرا سوکت به شما *نخواهد گفت* که دیگر چیزی برای خواندن وجود ندارد (فعلاً). حال اگر کمی درباره آن بیندیشید، به یک حقیقت بنیادی سوکتها پی خواهید برد: *پیامها باید یا طول ثابتی داشته باشند* (چندشآور)، *یا جداشده باشند* (شانه بالا انداختن)، *یا طول خود را مشخص کنند* (بسیار بهتر)، *یا با خاموش کردن اتصال پایانیابند*. انتخاب کاملاً با شماست، (اما برخی روشها از سایر روشها درستتر هستند)."
msgid "Assuming you don't want to end the connection, the simplest solution is a fixed length message::"
msgstr "با فرض اینکه نمیخواهید اتصال را پایان دهید، سادهترین راهحل، پیامی با طول ثابت است::"
msgid ""
"class MySocket:\n"
" \"\"\"demonstration class only\n"
" - coded for clarity, not efficiency\n"
" \"\"\"\n"
"\n"
" def __init__(self, sock=None):\n"
" if sock is None:\n"
" self.sock = socket.socket(\n"
" socket.AF_INET, socket.SOCK_STREAM)\n"
" else:\n"
" self.sock = sock\n"
"\n"
" def connect(self, host, port):\n"
" self.sock.connect((host, port))\n"
"\n"
" def mysend(self, msg):\n"
" totalsent = 0\n"
" while totalsent < MSGLEN:\n"
" sent = self.sock.send(msg[totalsent:])\n"
" if sent == 0:\n"
" raise RuntimeError(\"socket connection broken\")\n"
" totalsent = totalsent + sent\n"
"\n"
" def myreceive(self):\n"
" chunks = []\n"
" bytes_recd = 0\n"
" while bytes_recd < MSGLEN:\n"
" chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))\n"
" if chunk == b'':\n"
" raise RuntimeError(\"socket connection broken\")\n"
" chunks.append(chunk)\n"
" bytes_recd = bytes_recd + len(chunk)\n"
" return b''.join(chunks)"
msgstr ""
"class MySocket:\n"
" \"\"\"demonstration class only\n"
" - coded for clarity, not efficiency\n"
" \"\"\"\n"
"\n"
" def __init__(self, sock=None):\n"
" if sock is None:\n"
" self.sock = socket.socket(\n"
" socket.AF_INET, socket.SOCK_STREAM)\n"
" else:\n"
" self.sock = sock\n"
"\n"
" def connect(self, host, port):\n"
" self.sock.connect((host, port))\n"
"\n"
" def mysend(self, msg):\n"
" totalsent = 0\n"
" while totalsent < MSGLEN:\n"
" sent = self.sock.send(msg[totalsent:])\n"
" if sent == 0:\n"
" raise RuntimeError(\"socket connection broken\")\n"
" totalsent = totalsent + sent\n"
"\n"
" def myreceive(self):\n"
" chunks = []\n"
" bytes_recd = 0\n"
" while bytes_recd < MSGLEN:\n"
" chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))\n"
" if chunk == b'':\n"
" raise RuntimeError(\"socket connection broken\")\n"
" chunks.append(chunk)\n"
" bytes_recd = bytes_recd + len(chunk)\n"
" return b''.join(chunks)"
msgid "The sending code here is usable for almost any messaging scheme - in Python you send strings, and you can use ``len()`` to determine its length (even if it has embedded ``\\0`` characters). It's mostly the receiving code that gets more complex. (And in C, it's not much worse, except you can't use ``strlen`` if the message has embedded ``\\0``\\ s.)"
msgstr "کد ارسال در اینجا برای تقریباً هر روش پیامرسانی قابل استفاده است - در پایتون شما رشتهها را ارسال میکنید، و میتوانید از ``len()`` برای تعیین طول آنها استفاده کنید (حتی اگر دارای نویسههای ``\\0`` نهفته باشند). این عمدتاً کد دریافت است که پیچیدهتر میشود. (و در C، خیلی بدتر نیست، به جز اینکه اگر پیام دارای نویسههای ``\\0`` نهفته باشد، نمیتوانید از ``strlen`` استفاده کنید.)"
msgid "The easiest enhancement is to make the first character of the message an indicator of message type, and have the type determine the length. Now you have two ``recv``\\ s - the first to get (at least) that first character so you can look up the length, and the second in a loop to get the rest. If you decide to go the delimited route, you'll be receiving in some arbitrary chunk size, (4096 or 8192 is frequently a good match for network buffer sizes), and scanning what you've received for a delimiter."
msgstr "سادهترین بهبود این است که نخستین نویسهی پیام را نشانگر نوع پیام قرار دهید، و نوع، طول را تعیین کند. اکنون دو ``recv``\\ s دارید — اولی برای دریافت (حداقل) همان نخستین نویسه است تا بتوانید طول را پیدا کنید، و دومی در یک حلقه برای دریافت باقیمانده است. اگر تصمیم بگیرید از روش جداکنندهدار استفاده کنید، دریافت در اندازهی تکهی دلخواهی (chunk) انجام خواهد شد، (۴۰۹۶ یا ۸۱۹۲ اغلب تطابق خوبی با اندازههای بافر شبکه دارد)، و آنچه را دریافت کردهاید برای یافتن یک جداکننده پویش خواهید کرد."
msgid "One complication to be aware of: if your conversational protocol allows multiple messages to be sent back to back (without some kind of reply), and you pass ``recv`` an arbitrary chunk size, you may end up reading the start of a following message. You'll need to put that aside and hold onto it, until it's needed."
msgstr "یک پیچیدگی که باید از آن آگاه باشید: اگر پروتکل گفتگویی شما اجازه دهد چندین پیام بهصورت پشتسرهم (بدون دریافت پاسخی) ارسال شوند و به ``recv`` یک اندازهی تکهی دلخواه (chunk size) بدهید، ممکن است در نهایت آغاز پیام بعدی را بخوانید. باید آن را کنار بگذارید و نگه دارید، تا زمانی که به آن نیاز باشد."
msgid "Prefixing the message with its length (say, as 5 numeric characters) gets more complex, because (believe it or not), you may not get all 5 characters in one ``recv``. In playing around, you'll get away with it; but in high network loads, your code will very quickly break unless you use two ``recv`` loops - the first to determine the length, the second to get the data part of the message. Nasty. This is also when you'll discover that ``send`` does not always manage to get rid of everything in one pass. And despite having read this, you will eventually get bit by it!"
msgstr "قرار دادن طول پیام بهعنوان پیشوند آن (مثلاً بهصورت ۵ نویسه عددی) پیچیدهتر میشود، زیرا (باور کنید یا نه)، ممکن است همه ۵ نویسه را در یک ``recv`` دریافت نکنید. در آزمایشهای ساده، ممکن است مشکلی پیش نیاید؛ اما در بارهای شبکهای بالا، کد شما بسیار سریع از کار میافتد، مگر اینکه از دو حلقه ``recv`` استفاده کنید: اولی برای تعیین طول و دومی برای دریافت بخش دادهای پیام. آزاردهنده است. در همین زمان است که متوجه میشوید ``send`` همیشه نمیتواند همهچیز را در یک مرحله ارسال کند. و با وجود خواندن این مطلب، در نهایت گرفتار آن خواهید شد!"
msgid "In the interests of space, building your character, (and preserving my competitive position), these enhancements are left as an exercise for the reader. Lets move on to cleaning up."
msgstr "بهخاطر صرفهجویی در فضا، پرورش شخصیت شما، (و حفظ جایگاه رقابتی من)، این بهبودها بهعنوان تمرینی برای خواننده باقی گذاشته شدهاند. بیایید به پاکسازی بپردازیم."
msgid "Binary Data"
msgstr "دادههای دودویی"
msgid "It is perfectly possible to send binary data over a socket. The major problem is that not all machines use the same formats for binary data. For example, `network byte order <https://en.wikipedia.org/wiki/Endianness#Networking>`_ is big-endian, with the most significant byte first, so a 16 bit integer with the value ``1`` would be the two hex bytes ``00 01``. However, most common processors (x86/AMD64, ARM, RISC-V), are little-endian, with the least significant byte first - that same ``1`` would be ``01 00``."
msgstr "ارسال دادههای دودویی از طریق یک سوکت کاملاً ممکن است. مشکل اصلی این است که همهی ماشینها از قالبهای یکسانی برای دادههای دودویی استفاده نمیکنند. برای مثال، `ترتیب بایت شبکه <https://fa.wikipedia.org/wiki/اندیان#شبکه>`_ بهصورت بزرگاندیان (big-endian) است و پرارزشترین بایت در ابتدا قرار میگیرد؛ بنابراین یک عدد صحیح ۱۶ بیتی با مقدار ``1`` بهصورت دو بایت مبنای شانزده ``00 01`` خواهد بود. با این حال، بیشتر پردازندههای رایج (x86/AMD64، ARM، RISC-V) کوچکاندیان (little-endian) هستند و کمارزشترین بایت در ابتدا قرار میگیرد — همان ``1`` بهصورت ``01 00`` خواهد بود."
msgid "Socket libraries have calls for converting 16 and 32 bit integers - ``ntohl, htonl, ntohs, htons`` where \"n\" means *network* and \"h\" means *host*, \"s\" means *short* and \"l\" means *long*. Where network order is host order, these do nothing, but where the machine is byte-reversed, these swap the bytes around appropriately."
msgstr "کتابخانههای سوکت فراخوانیهایی برای تبدیل اعداد صحیح ۱۶ بیتی و ۳۲ بیتی دارند - ``ntohl, htonl, ntohs, htons`` که در آنها «n» به معنای *network* (شبکه)، «h» به معنای *host* (میزبان)، «s» به معنای *short* (کوتاه) و «l» به معنای *long* (بلند) است. هر جا که ترتیب شبکه همان ترتیب میزبان باشد، این فراخوانیها هیچ کاری انجام نمیدهند، اما هر جا که ترتیب بایتهای ماشین معکوس باشد، این فراخوانیها بایتها را بهشکل مناسب جابهجا میکنند."
msgid "In these days of 64-bit machines, the ASCII representation of binary data is frequently smaller than the binary representation. That's because a surprising amount of the time, most integers have the value 0, or maybe 1. The string ``\"0\"`` would be two bytes, while a full 64-bit integer would be 8. Of course, this doesn't fit well with fixed-length messages. Decisions, decisions."
msgstr "در این روزگار ماشینهای ۶۴ بیتی، نمایش ASCII دادههای دودویی اغلب کوچکتر از نمایش دودویی آنها است. دلیلش این است که در کمال تعجب، در بیشتر مواقع، اغلب اعداد صحیح مقدار ۰ دارند، یا شاید ۱. رشتهی ``\"0\"`` دو بایت خواهد بود، در حالی که یک عدد صحیح ۶۴ بیتی کامل ۸ بایت خواهد بود. البته، این موضوع با پیامهای با طول ثابت سازگاری خوبی ندارد. تصمیمها، تصمیمها."
msgid "Disconnecting"
msgstr "قطع اتصال"
msgid "Strictly speaking, you're supposed to use ``shutdown`` on a socket before you ``close`` it. The ``shutdown`` is an advisory to the socket at the other end. Depending on the argument you pass it, it can mean \"I'm not going to send anymore, but I'll still listen\", or \"I'm not listening, good riddance!\". Most socket libraries, however, are so used to programmers neglecting to use this piece of etiquette that normally a ``close`` is the same as ``shutdown(); close()``. So in most situations, an explicit ``shutdown`` is not needed."
msgstr "اگر بخواهیم دقیق باشیم، شما باید پیش از ``close`` کردن یک سوکت، از ``shutdown`` بر روی آن استفاده کنید. ``shutdown`` یک اعلان به سوکت سمت دیگر است. بسته به آرگومانی که به آن میدهید، میتواند به معنای «دیگر چیزی ارسال نخواهم کرد، اما همچنان گوش میدهم» یا «دیگر گوش نمیدهم، راحت شدم!» باشد. با این حال، بیشتر کتابخانههای سوکت چنان به غفلت برنامهنویسان از رعایت این آداب عادت کردهاند که معمولاً ``close`` همان ``shutdown(); close()`` است. بنابراین در بیشتر موارد، به ``shutdown`` صریح نیازی نیست."
msgid "One way to use ``shutdown`` effectively is in an HTTP-like exchange. The client sends a request and then does a ``shutdown(1)``. This tells the server \"This client is done sending, but can still receive.\" The server can detect \"EOF\" by a receive of 0 bytes. It can assume it has the complete request. The server sends a reply. If the ``send`` completes successfully then, indeed, the client was still receiving."
msgstr "یکی از راههای استفادهی مؤثر از ``shutdown``، در یک تبادل شبیه به HTTP است. کلاینت درخواستی ارسال میکند و سپس ``shutdown(1)`` را فراخوانی میکند. این کار به سرور میگوید: «این کلاینت ارسال را به پایان رسانده است، اما هنوز میتواند دریافت کند.» سرور میتواند با دریافت ۰ بایت، «EOF» را تشخیص دهد. میتواند فرض کند که درخواست کامل را دریافت کرده است. سرور پاسخی میفرستد. اگر ``send`` با موفقیت کامل شود، در واقع کلاینت هنوز در حال دریافت بوده است."
msgid "Python takes the automatic shutdown a step further, and says that when a socket is garbage collected, it will automatically do a ``close`` if it's needed. But relying on this is a very bad habit. If your socket just disappears without doing a ``close``, the socket at the other end may hang indefinitely, thinking you're just being slow. *Please* ``close`` your sockets when you're done."
msgstr "پایتون خاموش شدن خودکار را یک گام فراتر میبرد و میگوید که وقتی یک سوکت زبالهروبی میشود، در صورت نیاز بهطور خودکار ``close`` را انجام میدهد. اما اتکا به این موضوع عادت بسیار بدی است. اگر سوکت شما بدون انجام ``close`` صرفاً ناپدید شود، سوکت سمت مقابل ممکن است بهطور نامعلومی معلق بماند، به این گمان که شما صرفاً کند هستید. *لطفاً* پس از اتمام کار، سوکتهای خود را ``close`` کنید."
msgid "When Sockets Die"
msgstr "هنگامی که سوکتها میمیرند"
msgid "Probably the worst thing about using blocking sockets is what happens when the other side comes down hard (without doing a ``close``). Your socket is likely to hang. TCP is a reliable protocol, and it will wait a long, long time before giving up on a connection. If you're using threads, the entire thread is essentially dead. There's not much you can do about it. As long as you aren't doing something dumb, like holding a lock while doing a blocking read, the thread isn't really consuming much in the way of resources. Do *not* try to kill the thread - part of the reason that threads are more efficient than processes is that they avoid the overhead associated with the automatic recycling of resources. In other words, if you do manage to kill the thread, your whole process is likely to be screwed up."
msgstr "احتمالاً بدترین نکته در استفاده از سوکتهای مسدودکننده همان چیزی است که وقتی طرف مقابل بهطور ناگهانی از کار میافتد (بدون انجام یک ``close``) رخ میدهد. بهاحتمال زیاد سوکت شما آویزان میشود. TCP پروتکلی قابلاعتماد است و مدت بسیار، بسیار طولانی منتظر میماند تا از یک اتصال دست بکشد. اگر از نخها استفاده میکنید، آن نخ عملاً بهطور کامل مرده است. کار زیادی از دست شما برنمیآید. تا زمانی که کار احمقانهای مانند نگهداشتن یک قفل هنگام انجام یک خواندن مسدودکننده انجام نمیدهید، نخ واقعاً منابع زیادی مصرف نمیکند. سعی *نکنید* نخ را بکشید — بخشی از دلیل کارآمدتر بودن نخها نسبت به فرایندها این است که آنها از سربار مرتبط با بازیافت خودکار منابع اجتناب میکنند. به عبارت دیگر، اگر واقعاً موفق به کشتن نخ شوید، احتمالاً کل فرایند شما دچار مشکل جدی میشود."
msgid "Non-blocking Sockets"
msgstr "سوکتهای غیرمسدودکننده"
msgid "If you've understood the preceding, you already know most of what you need to know about the mechanics of using sockets. You'll still use the same calls, in much the same ways. It's just that, if you do it right, your app will be almost inside-out."
msgstr "اگر مطالب پیشین را درک کرده باشید، از پیش بیشترِ آنچه را که باید دربارهی سازوکار استفاده از سوکتها بدانید، میدانید. شما همچنان از همان فراخوانیها، به تقریباً همان شیوهها استفاده خواهید کرد. فقط این است که اگر این کار را درست انجام دهید، برنامهی شما تقریباً وارونه خواهد شد."
msgid "In Python, you use ``socket.setblocking(False)`` to make it non-blocking. In C, it's more complex, (for one thing, you'll need to choose between the BSD flavor ``O_NONBLOCK`` and the almost indistinguishable POSIX flavor ``O_NDELAY``, which is completely different from ``TCP_NODELAY``), but it's the exact same idea. You do this after creating the socket, but before using it. (Actually, if you're nuts, you can switch back and forth.)"
msgstr "در پایتون، برای غیرمسدود کردن آن از ``socket.setblocking(False)`` استفاده میکنید. در C، این کار پیچیدهتر است، (برای مثال، باید بین نوع BSD یعنی ``O_NONBLOCK`` و نوع POSIX یعنی ``O_NDELAY`` که تقریباً غیرقابلتشخیص است و کاملاً با ``TCP_NODELAY`` تفاوت دارد، یکی را انتخاب کنید)، اما ایده دقیقاً همان است. شما این کار را پس از ایجاد سوکت، اما پیش از استفاده از آن انجام میدهید. (البته اگر دیوانه باشید، میتوانید بین این دو حالت جابهجا شوید.)"
msgid "The major mechanical difference is that ``send``, ``recv``, ``connect`` and ``accept`` can return without having done anything. You have (of course) a number of choices. You can check return code and error codes and generally drive yourself crazy. If you don't believe me, try it sometime. Your app will grow large, buggy and suck CPU. So let's skip the brain-dead solutions and do it right."
msgstr "تفاوت سازوکاری اصلی این است که ``send``، ``recv``، ``connect`` و ``accept`` میتوانند بدون اینکه کاری انجام داده باشند، بازگردند. شما (البته) گزینههای متعددی دارید. میتوانید کد بازگشتی و کدهای خطا را بررسی کنید و بهطور کلی خود را دیوانه کنید. اگر باور نمیکنید، گاهی آن را امتحان کنید. برنامهتان بزرگ و پر از اشکال خواهد شد و منابع پردازنده را بهشدت مصرف خواهد کرد. پس بیایید راهحلهای نابخردانه را کنار بگذاریم و کار را درست انجام دهیم."
msgid "Use ``select``."
msgstr "از ``select`` استفاده کنید."
msgid "In C, coding ``select`` is fairly complex. In Python, it's a piece of cake, but it's close enough to the C version that if you understand ``select`` in Python, you'll have little trouble with it in C::"
msgstr "در C، کدنویسی ``select`` نسبتاً پیچیده است. در پایتون، این کار بسیار آسان است، اما به اندازه کافی به نسخه C نزدیک است که اگر ``select`` را در پایتون درک کنید، در C با مشکل چندانی مواجه نخواهید شد::"
msgid ""
"ready_to_read, ready_to_write, in_error = \\\n"
" select.select(\n"
" potential_readers,\n"
" potential_writers,\n"
" potential_errs,\n"
" timeout)"
msgstr ""
"ready_to_read, ready_to_write, in_error = \\\n"
" select.select(\n"
" potential_readers,\n"
" potential_writers,\n"
" potential_errs,\n"
" timeout)"
msgid "You pass ``select`` three lists: the first contains all sockets that you might want to try reading; the second all the sockets you might want to try writing to, and the last (normally left empty) those that you want to check for errors. You should note that a socket can go into more than one list. The ``select`` call is blocking, but you can give it a timeout. This is generally a sensible thing to do - give it a nice long timeout (say a minute) unless you have good reason to do otherwise."
msgstr "شما به ``select`` سه فهرست میدهید: اولی شامل همه سوکتهایی است که ممکن است بخواهید خواندن از آنها را امتحان کنید؛ دومی شامل همه سوکتهایی است که ممکن است بخواهید نوشتن به آنها را امتحان کنید، و سومی (که معمولاً خالی گذاشته میشود) شامل سوکتهایی است که میخواهید آنها را از نظر خطاها بررسی کنید. باید توجه داشته باشید که یک سوکت میتواند در بیش از یک فهرست قرار بگیرد. فراخوانی ``select`` مسدودکننده است، اما میتوانید برای آن یک مهلت تعیین کنید. این کار معمولاً عاقلانهای است — مگر اینکه دلیل خوبی برای انجام خلاف این کار داشته باشید، یک مهلت طولانی و مناسب برای آن تعیین کنید (مثلاً یک دقیقه)."
msgid "In return, you will get three lists. They contain the sockets that are actually readable, writable and in error. Each of these lists is a subset (possibly empty) of the corresponding list you passed in."
msgstr "در ازای آن، سه فهرست دریافت خواهید کرد. این فهرستها شامل سوکتهایی هستند که در واقع قابلخواندن، قابلنوشتن و دارای خطا هستند. هر یک از این فهرستها زیرمجموعهای (احتمالاً خالی) از فهرست متناظری است که شما بهعنوان ورودی دادهاید."
msgid "If a socket is in the output readable list, you can be as-close-to-certain-as-we-ever-get-in-this-business that a ``recv`` on that socket will return *something*. Same idea for the writable list. You'll be able to send *something*. Maybe not all you want to, but *something* is better than nothing. (Actually, any reasonably healthy socket will return as writable - it just means outbound network buffer space is available.)"
msgstr "اگر یک سوکت در فهرست خروجی خواندنی باشد، میتوانید تا حدی که در این کار ممکن است مطمئن باشید که یک ``recv`` روی آن سوکت *چیزی* را برمیگرداند. همین مفهوم برای فهرست نوشتنی نیز صادق است. شما میتوانید *چیزی* ارسال کنید. شاید نه همهی آن چیزی که میخواهید، اما *چیزی* بهتر از هیچ است. (در واقع، هر سوکت نسبتاً سالمی بهعنوان نوشتنی بازگردانده میشود؛ این صرفاً به این معناست که فضای بافر شبکهی خروجی در دسترس است.)"
msgid "If you have a \"server\" socket, put it in the potential_readers list. If it comes out in the readable list, your ``accept`` will (almost certainly) work. If you have created a new socket to ``connect`` to someone else, put it in the potential_writers list. If it shows up in the writable list, you have a decent chance that it has connected."
msgstr "اگر یک سوکت «سرور» دارید، آن را در فهرست potential_readers قرار دهید. اگر در فهرست readable ظاهر شود، ``accept`` شما (تقریباً بهطور قطع) کار خواهد کرد. اگر یک سوکت جدید برای ``connect`` به طرف دیگر ایجاد کردهاید، آن را در فهرست potential_writers قرار دهید. اگر در فهرست writable ظاهر شود، احتمال مناسبی وجود دارد که متصل شده باشد."
msgid "Actually, ``select`` can be handy even with blocking sockets. It's one way of determining whether you will block - the socket returns as readable when there's something in the buffers. However, this still doesn't help with the problem of determining whether the other end is done, or just busy with something else."
msgstr "در واقع، ``select`` حتی با سوکتهای مسدودکننده هم میتواند مفید باشد. این یکی از راههای تعیین این است که آیا مسدود خواهید شد یا خیر؛ هنگامی که چیزی در بافرها وجود دارد، سوکت بهعنوان قابلخواندن بازگردانده میشود. با این حال، این هنوز به مسئلهی تعیین اینکه آیا طرف مقابل کار را تمام کرده است یا صرفاً مشغول کار دیگری است، کمکی نمیکند."
msgid "**Portability alert**: On Unix, ``select`` works both with the sockets and files. Don't try this on Windows. On Windows, ``select`` works with sockets only. Also note that in C, many of the more advanced socket options are done differently on Windows. In fact, on Windows I usually use threads (which work very, very well) with my sockets."
msgstr "**هشدار قابلیت حمل**: در یونیکس، ``select`` هم با سوکتها و هم با پروندهها کار میکند. این کار را در ویندوز امتحان نکنید. در ویندوز، ``select`` فقط با سوکتها کار میکند. همچنین توجه داشته باشید که در C، بسیاری از گزینههای پیشرفتهتر سوکت در ویندوز به شکل متفاوتی انجام میشوند. در واقع، در ویندوز من معمولاً از نخها (که بسیار، بسیار خوب کار میکنند) همراه با سوکتهایم استفاده میکنم."