Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e37fc18b
Kaydet (Commit)
e37fc18b
authored
Nis 24, 2016
tarafından
Martin Panter
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #24911: All socket objects are context managers; update examples
üst
887bc96e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
37 deletions
+30
-37
socket.rst
Doc/library/socket.rst
+27
-27
socketserver.rst
Doc/library/socketserver.rst
+3
-10
No files found.
Doc/library/socket.rst
Dosyayı görüntüle @
e37fc18b
...
@@ -445,9 +445,6 @@ The following functions all create :ref:`socket objects <socket-objects>`.
...
@@ -445,9 +445,6 @@ The following functions all create :ref:`socket objects <socket-objects>`.
.. versionchanged:: 3.2
.. versionchanged:: 3.2
*source_address* was added.
*source_address* was added.
.. versionchanged:: 3.2
support for the :keyword:`with` statement was added.
.. function:: fromfd(fd, family, type, proto=0)
.. function:: fromfd(fd, family, type, proto=0)
...
@@ -831,6 +828,10 @@ Socket objects have the following methods. Except for
...
@@ -831,6 +828,10 @@ Socket objects have the following methods. Except for
:meth:`~socket.makefile`, these correspond to Unix system calls applicable
:meth:`~socket.makefile`, these correspond to Unix system calls applicable
to sockets.
to sockets.
.. versionchanged:: 3.2
Support for the :term:`context manager` protocol was added. Exiting the
context manager is equivalent to calling :meth:`~socket.close`.
.. method:: socket.accept()
.. method:: socket.accept()
...
@@ -1457,16 +1458,16 @@ The first two examples support IPv4 only. ::
...
@@ -1457,16 +1458,16 @@ The first two examples support IPv4 only. ::
HOST = '' # Symbolic name meaning all available interfaces
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.bind((HOST, PORT))
s.listen(1)
s.listen(1)
conn, addr = s.accept()
conn, addr = s.accept()
print('Connected by', addr)
with conn:
while True:
print('Connected by', addr)
data = conn.recv(1024)
while True:
if not data: break
data = conn.recv(1024)
conn.sendall(data)
if not data: break
conn.close(
)
conn.sendall(data
)
::
::
...
@@ -1475,11 +1476,10 @@ The first two examples support IPv4 only. ::
...
@@ -1475,11 +1476,10 @@ The first two examples support IPv4 only. ::
HOST = 'daring.cwi.nl' # The remote host
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
s.sendall(b'Hello, world')
data = s.recv(1024)
data = s.recv(1024)
s.close()
print('Received', repr(data))
print('Received', repr(data))
The next two examples are identical to the above two, but support both IPv4 and
The next two examples are identical to the above two, but support both IPv4 and
...
@@ -1516,12 +1516,12 @@ sends traffic to the first one connected successfully. ::
...
@@ -1516,12 +1516,12 @@ sends traffic to the first one connected successfully. ::
print('could not open socket')
print('could not open socket')
sys.exit(1)
sys.exit(1)
conn, addr = s.accept()
conn, addr = s.accept()
print('Connected by', addr)
with conn:
while True:
print('Connected by', addr)
data = conn.recv(1024)
while True:
if not data: break
data = conn.recv(1024)
conn.send(data)
if not data: break
conn.close(
)
conn.send(data
)
::
::
...
@@ -1549,9 +1549,9 @@ sends traffic to the first one connected successfully. ::
...
@@ -1549,9 +1549,9 @@ sends traffic to the first one connected successfully. ::
if s is None:
if s is None:
print('could not open socket')
print('could not open socket')
sys.exit(1)
sys.exit(1)
s.sendall(b'Hello, world')
with s:
data = s.recv(1024
)
s.sendall(b'Hello, world'
)
s.close(
)
data = s.recv(1024
)
print('Received', repr(data))
print('Received', repr(data))
...
...
Doc/library/socketserver.rst
Dosyayı görüntüle @
e37fc18b
...
@@ -465,17 +465,13 @@ This is the client side::
...
@@ -465,17 +465,13 @@ This is the client side::
data = " ".join(sys.argv[1:])
data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
# Connect to server and send data
# Connect to server and send data
sock.connect((HOST, PORT))
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(data))
print("Sent: {}".format(data))
print("Received: {}".format(received))
print("Received: {}".format(received))
...
@@ -574,14 +570,11 @@ An example for the :class:`ThreadingMixIn` class::
...
@@ -574,14 +570,11 @@ An example for the :class:`ThreadingMixIn` class::
pass
pass
def client(ip, port, message):
def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.connect((ip, port))
try:
sock.sendall(bytes(message, 'ascii'))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
print("Received: {}".format(response))
finally:
sock.close()
if __name__ == "__main__":
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
# Port 0 means to select an arbitrary unused port
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment