Use getaddrinfo to create the right socket

This commit is contained in:
nemunaire 2016-08-10 23:56:50 +02:00
parent 1f5cfb2ead
commit d4302780da
2 changed files with 7 additions and 13 deletions

View File

@ -16,6 +16,7 @@
from datetime import datetime from datetime import datetime
import re import re
import socket
from nemubot.channel import Channel from nemubot.channel import Channel
from nemubot.message.printer.IRC import IRC as IRCPrinter from nemubot.message.printer.IRC import IRC as IRCPrinter
@ -240,7 +241,7 @@ class _IRC:
if self.capabilities is not None: if self.capabilities is not None:
self.write("CAP LS") self.write("CAP LS")
self.write("NICK :" + self.nick) self.write("NICK :" + self.nick)
self.write("USER %s %s bla :%s" % (self.username, self.host, self.realname)) self.write("USER %s %s bla :%s" % (self.username, socket.getfqdn(), self.realname))
def close(self): def close(self):

View File

@ -81,24 +81,17 @@ class _Socket(AbstractServer):
class _SocketServer(_Socket): class _SocketServer(_Socket):
def __init__(self, host, port, bind=None, **kwargs): def __init__(self, host, port, bind=None, **kwargs):
super().__init__(family=socket.AF_INET, **kwargs) (family, type, proto, canonname, sockaddr) = socket.getaddrinfo(host, port)[0]
assert(host is not None) super().__init__(family=family, type=type, proto=proto, **kwargs)
assert(isinstance(port, int))
self._host = host self._sockaddr = sockaddr
self._port = port
self._bind = bind self._bind = bind
@property
def host(self):
return self._host
def connect(self): def connect(self):
self.logger.info("Connection to %s:%d", self._host, self._port) self.logger.info("Connection to %s:%d", *self._sockaddr[:2])
super().connect((self._host, self._port)) super().connect(self._sockaddr)
if self._bind: if self._bind:
super().bind(self._bind) super().bind(self._bind)