Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b6da0006
Commit
b6da0006
authored
Jul 24, 2001
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
parent
1d86081c
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
160 additions
and
49 deletions
+160
-49
Lib/BaseHTTPServer.py
Lib/BaseHTTPServer.py
+4
-1
Lib/SocketServer.py
Lib/SocketServer.py
+1
-1
Lib/ftplib.py
Lib/ftplib.py
+90
-22
Lib/httplib.py
Lib/httplib.py
+16
-4
Lib/poplib.py
Lib/poplib.py
+17
-7
Lib/smtplib.py
Lib/smtplib.py
+20
-12
Lib/telnetlib.py
Lib/telnetlib.py
+12
-2
No files found.
Lib/BaseHTTPServer.py
View file @
b6da0006
...
...
@@ -68,8 +68,10 @@ __all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
import
sys
import
time
import
socket
# For gethostbyaddr()
import
string
import
mimetools
import
SocketServer
import
re
# Default error message
DEFAULT_ERROR_MESSAGE
=
"""
\
...
...
@@ -474,7 +476,8 @@ def test(HandlerClass = BaseHTTPRequestHandler,
httpd
=
ServerClass
(
server_address
,
HandlerClass
)
print
"Serving HTTP on port"
,
port
,
"..."
sa
=
httpd
.
socket
.
getsockname
()
print
"Serving HTTP on"
,
sa
[
0
],
"port"
,
sa
[
1
],
"..."
httpd
.
serve_forever
()
...
...
Lib/SocketServer.py
View file @
b6da0006
...
...
@@ -5,7 +5,7 @@ This module tries to capture the various aspects of defining a server:
For socket-based servers:
- address family:
- AF_INET: IP (Internet Protocol) sockets (default)
- AF_INET
{,6}
: IP (Internet Protocol) sockets (default)
- AF_UNIX: Unix domain sockets
- others, e.g. AF_DECNET are conceivable (see <socket.h>
- socket type:
...
...
Lib/ftplib.py
View file @
b6da0006
...
...
@@ -108,14 +108,26 @@ class FTP:
self
.
connect
(
host
)
if
user
:
self
.
login
(
user
,
passwd
,
acct
)
def
connect
(
self
,
host
=
''
,
port
=
0
):
def
connect
(
self
,
host
=
''
,
port
=
0
):
'''Connect to host. Arguments are:
- host: hostname to connect to (string, default previous host)
- port: port to connect to (integer, default previous port)'''
if
host
:
self
.
host
=
host
if
port
:
self
.
port
=
port
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
.
connect
((
self
.
host
,
self
.
port
))
self
.
passiveserver
=
0
for
res
in
socket
.
getaddrinfo
(
self
.
host
,
self
.
port
,
0
,
socket
.
SOCK_STREAM
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
try
:
self
.
sock
=
socket
.
socket
(
af
,
socktype
,
proto
)
self
.
sock
.
connect
(
sa
)
except
socket
.
error
,
msg
:
self
.
sock
.
close
()
self
.
sock
=
None
continue
break
if
not
self
.
sock
:
raise
socket
.
error
,
msg
self
.
af
=
af
self
.
file
=
self
.
sock
.
makefile
(
'rb'
)
self
.
welcome
=
self
.
getresp
()
return
self
.
welcome
...
...
@@ -243,16 +255,49 @@ class FTP:
cmd
=
'PORT '
+
','
.
join
(
bytes
)
return
self
.
voidcmd
(
cmd
)
def
sendeprt
(
self
,
host
,
port
):
'''Send a EPRT command with the current host and the given port number.'''
af
=
0
if
self
.
af
==
socket
.
AF_INET
:
af
=
1
if
self
.
af
==
socket
.
AF_INET6
:
af
=
2
if
af
==
0
:
raise
error_proto
,
'unsupported address family'
fields
=
[
''
,
`af`
,
host
,
`port`
,
''
]
cmd
=
'EPRT '
+
string
.
joinfields
(
fields
,
'|'
)
return
self
.
voidcmd
(
cmd
)
def
makeport
(
self
):
'''Create a new socket and send a PORT command for it.'''
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
.
bind
((
''
,
0
))
for
res
in
socket
.
getaddrinfo
(
None
,
0
,
self
.
af
,
socket
.
SOCK_STREAM
,
0
,
socket
.
AI_PASSIVE
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
try
:
sock
=
socket
.
socket
(
af
,
socktype
,
proto
)
sock
.
bind
(
sa
)
except
socket
.
error
,
msg
:
sock
.
close
()
sock
=
None
continue
break
if
not
sock
:
raise
socket
.
error
,
msg
sock
.
listen
(
1
)
dummyhost
,
port
=
sock
.
getsockname
()
# Get proper port
host
,
dummyport
=
self
.
sock
.
getsockname
()
# Get proper host
port
=
sock
.
getsockname
()[
1
]
# Get proper port
host
=
self
.
sock
.
getsockname
()[
0
]
# Get proper host
if
self
.
af
==
socket
.
AF_INET
:
resp
=
self
.
sendport
(
host
,
port
)
else
:
resp
=
self
.
sendeprt
(
host
,
port
)
return
sock
def
makepasv
(
self
):
if
self
.
af
==
socket
.
AF_INET
:
host
,
port
=
parse227
(
self
.
sendcmd
(
'PASV'
))
else
:
host
,
port
=
parse229
(
self
.
sendcmd
(
'EPSV'
),
self
.
sock
.
getpeername
())
return
host
,
port
def
ntransfercmd
(
self
,
cmd
,
rest
=
None
):
"""Initiate a transfer over the data connection.
...
...
@@ -270,9 +315,10 @@ class FTP:
"""
size
=
None
if
self
.
passiveserver
:
host
,
port
=
parse227
(
self
.
sendcmd
(
'PASV'
))
conn
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
conn
.
connect
((
host
,
port
))
host
,
port
=
self
.
makepasv
()
af
,
socktype
,
proto
,
canon
,
sa
=
socket
.
getaddrinfo
(
host
,
port
,
0
,
socket
.
SOCK_STREAM
)[
0
]
conn
=
socket
.
socket
(
af
,
socktype
,
proto
)
conn
.
connect
(
sa
)
if
rest
is
not
None
:
self
.
sendcmd
(
"REST %s"
%
rest
)
resp
=
self
.
sendcmd
(
cmd
)
...
...
@@ -523,6 +569,28 @@ def parse227(resp):
return host, port
def parse229(resp, peer):
'''Parse the '229' response for a EPSV request.
Raises error_proto if it does not contain '(|||port|)'
Return ('host.addr.as.numbers', port#) tuple.'''
if resp[:3] <> '229':
raise error_reply, resp
left = string.find(resp, '(')
if left < 0: raise error_proto, resp
right = string.find(resp, ')', left + 1)
if right < 0:
raise error_proto, resp # should contain '(|||port|)'
if resp[left + 1] <> resp[right - 1]:
raise error_proto, resp
parts = string.split(resp[left + 1:right], resp[left+1])
if len(parts) <> 5:
raise error_proto, resp
host = peer[0]
port = string.atoi(parts[3])
return host, port
def parse257(resp):
'''Parse the '257' response for a MKD or PWD request.
This is a response to a MKD or PWD request: a directory name.
...
...
Lib/httplib.py
View file @
b6da0006
...
...
@@ -357,10 +357,22 @@ class HTTPConnection:
def
connect
(
self
):
"""Connect to the host and port specified in __init__."""
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
for
res
in
socket
.
getaddrinfo
(
self
.
host
,
self
.
port
,
0
,
socket
.
SOCK_STREAM
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
try
:
self
.
sock
=
socket
.
socket
(
af
,
socktype
,
proto
)
if
self
.
debuglevel
>
0
:
print
"connect: (%s, %s)"
%
(
self
.
host
,
self
.
port
)
self
.
sock
.
connect
((
self
.
host
,
self
.
port
))
self
.
sock
.
connect
(
sa
)
except
socket
.
error
,
msg
:
if
self
.
debuglevel
>
0
:
print
'connect fail:'
,
(
self
.
host
,
self
.
port
)
self
.
sock
.
close
()
self
.
sock
=
None
continue
break
if
not
self
.
sock
:
raise
socket
.
error
,
msg
def
close
(
self
):
"""Close the connection to the HTTP server."""
...
...
Lib/poplib.py
View file @
b6da0006
...
...
@@ -75,8 +75,18 @@ class POP3:
def
__init__
(
self
,
host
,
port
=
POP3_PORT
):
self
.
host
=
host
self
.
port
=
port
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
.
connect
((
self
.
host
,
self
.
port
))
for
res
in
socket
.
getaddrinfo
(
self
.
host
,
self
.
port
,
0
,
socket
.
SOCK_STREAM
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
try
:
self
.
sock
=
socket
.
socket
(
af
,
socktype
,
proto
)
self
.
sock
.
connect
(
sa
)
except
socket
.
error
,
msg
:
self
.
sock
.
close
()
self
.
sock
=
None
continue
break
if
not
self
.
sock
:
raise
socket
.
error
,
msg
self
.
file
=
self
.
sock
.
makefile
(
'rb'
)
self
.
_debugging
=
0
self
.
welcome
=
self
.
_getresp
()
...
...
Lib/smtplib.py
View file @
b6da0006
...
...
@@ -208,24 +208,32 @@ class SMTP:
specified during instantiation.
"""
if not port
:
i = host.find('
:
')
if not port and (host.find('
:
') == host.rfind('
:
'))
:
i = host.
r
find('
:
')
if i >= 0:
host, port = host[:i], host[i+1:]
try: port = int(port)
except ValueError:
raise socket.error, "nonnumeric port"
if not port: port = SMTP_PORT
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.debuglevel > 0: print '
connect
:
', (host, port)
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock.connect((host, port))
except socket.error:
self.close()
raise
(code,msg)=self.getreply()
if self.debuglevel >0 : print "connect:", msg
return (code,msg)
self.sock = socket.socket(af, socktype, proto)
if self.debuglevel > 0: print '
connect
:
', (host, port)
self.sock.connect(sa)
except socket.error, msg:
if self.debuglevel > 0: print '
connect
fail
:
', (host, port)
self.sock.close()
self.sock = None
continue
break
if not self.sock:
raise socket.error, msg
(code, msg) = self.getreply()
if self.debuglevel > 0: print "connect:", msg
return (code, msg)
def send(self, str):
"""Send `str'
to
the
server
.
"""
...
...
Lib/telnetlib.py
View file @
b6da0006
...
...
@@ -136,8 +136,18 @@ class Telnet:
port
=
TELNET_PORT
self
.
host
=
host
self
.
port
=
port
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
.
connect
((
self
.
host
,
self
.
port
))
for
res
in
socket
.
getaddrinfo
(
host
,
port
,
0
,
socket
.
SOCK_STREAM
):
af
,
socktype
,
proto
,
canonname
,
sa
=
res
try
:
self
.
sock
=
socket
.
socket
(
af
,
socktype
,
proto
)
self
.
sock
.
connect
(
sa
)
except
socket
.
error
,
msg
:
self
.
sock
.
close
()
self
.
sock
=
None
continue
break
if
not
self
.
sock
:
raise
socket
.
error
,
msg
def
__del__
(
self
):
"""Destructor -- close the connection."""
...
...
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