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
79a3eb10
Kaydet (Commit)
79a3eb10
authored
15 years ago
tarafından
Gregory P. Smith
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Adds an optional source_address parameter to socket.create_connection().
For use by issue3972.
üst
7f8ebdba
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
5 deletions
+32
-5
socket.rst
Doc/library/socket.rst
+7
-1
socket.py
Lib/socket.py
+9
-3
test_socket.py
Lib/test/test_socket.py
+14
-1
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/socket.rst
Dosyayı görüntüle @
79a3eb10
...
...
@@ -205,7 +205,7 @@ The module :mod:`socket` exports the following constants and functions:
.. versionadded:: 2.3
.. function:: create_connection(address[, timeout])
.. function:: create_connection(address[, timeout
[, source_address]
])
Convenience function. Connect to *address* (a 2-tuple ``(host, port)``),
and return the socket object. Passing the optional *timeout* parameter will
...
...
@@ -215,6 +215,12 @@ The module :mod:`socket` exports the following constants and functions:
.. versionadded:: 2.6
If supplied, *source_address* must be a 2-tuple ``(host, port)`` for the
socket to bind to as its source address before connecting. If host or port
are '' or 0 respectively the OS default behavior will be used.
.. versionadded:: 2.7
.. function:: getaddrinfo(host, port[, family[, socktype[, proto[, flags]]]])
...
...
This diff is collapsed.
Click to expand it.
Lib/socket.py
Dosyayı görüntüle @
79a3eb10
...
...
@@ -24,7 +24,8 @@ inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
ssl() -- secure socket layer support (only available if configured)
socket.getdefaulttimeout() -- get the default timeout value
socket.setdefaulttimeout() -- set the default timeout value
create_connection() -- connects to an address, with an optional timeout
create_connection() -- connects to an address, with an optional timeout and
optional source address.
[*] not available on all platforms!
...
...
@@ -531,7 +532,8 @@ class _fileobject(object):
_GLOBAL_DEFAULT_TIMEOUT
=
object
()
def
create_connection
(
address
,
timeout
=
_GLOBAL_DEFAULT_TIMEOUT
):
def
create_connection
(
address
,
timeout
=
_GLOBAL_DEFAULT_TIMEOUT
,
source_address
=
None
):
"""Connect to *address* and return the socket object.
Convenience function. Connect to *address* (a 2-tuple ``(host,
...
...
@@ -539,7 +541,9 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
*timeout* parameter will set the timeout on the socket instance
before attempting to connect. If no *timeout* is supplied, the
global default timeout setting returned by :func:`getdefaulttimeout`
is used.
is used. If *source_address* is set it must be a tuple of (host, port)
for the socket to bind as a source address before making the connection.
An host of '' or port 0 tells the OS to use the default.
"""
msg
=
"getaddrinfo returns an empty list"
...
...
@@ -551,6 +555,8 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
sock
=
socket
(
af
,
socktype
,
proto
)
if
timeout
is
not
_GLOBAL_DEFAULT_TIMEOUT
:
sock
.
settimeout
(
timeout
)
if
source_address
:
sock
.
bind
(
source_address
)
sock
.
connect
(
sa
)
return
sock
...
...
This diff is collapsed.
Click to expand it.
Lib/test/test_socket.py
Dosyayı görüntüle @
79a3eb10
...
...
@@ -995,7 +995,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
ThreadableTest
.
__init__
(
self
)
def
clientSetUp
(
self
):
pass
self
.
source_port
=
test_support
.
find_unused_port
()
def
clientTearDown
(
self
):
self
.
cli
.
close
()
...
...
@@ -1010,6 +1010,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
)
self
.
assertEqual
(
self
.
cli
.
family
,
2
)
testSourcePort
=
_justAccept
def
_testSourcePort
(
self
):
self
.
cli
=
socket
.
create_connection
((
HOST
,
self
.
port
),
timeout
=
30
,
source_address
=
(
''
,
self
.
source_port
))
self
.
assertEqual
(
self
.
cli
.
getsockname
()[
1
],
self
.
source_port
)
testSourceAddress
=
_justAccept
def
_testSourceAddress
(
self
):
self
.
cli
=
socket
.
create_connection
(
(
HOST
,
self
.
port
),
30
,
(
'127.0.0.1'
,
self
.
source_port
))
self
.
assertEqual
(
self
.
cli
.
getsockname
(),
(
'127.0.0.1'
,
self
.
source_port
))
testTimeoutDefault
=
_justAccept
def
_testTimeoutDefault
(
self
):
# passing no explicit timeout uses socket's global default
...
...
This diff is collapsed.
Click to expand it.
Misc/NEWS
Dosyayı görüntüle @
79a3eb10
...
...
@@ -62,6 +62,8 @@ Core and Builtins
Library
-------
- socket.create_connection now accepts an optional source_address parameter.
- Issue #5511: now zipfile.ZipFile can be used as a context manager.
Initial patch by Brian Curtin.
...
...
This diff is collapsed.
Click to expand it.
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