Hi,
I'm trying to get some IPV6 python code running under
Windows. I have installed Python 2.5.1 for Windows
using the binaries from python.org. I'm a newbie to
Python programming as well.
The code works fine under Debian and MacOSX (both
using Python 2.5)
I have rebuilt the python binaries from source and set
ENABLE_IPV6. This didn't help. I have also read about
not being able to bind to a IPV4 and IPV6 socket. I
tried setting the setsockopt() call using IPV6_V6ONLY,
but this is not recognized by the python interpreter.
Thanks for any help I can get. I'm stuck on this and
searching the NET for help.
I get the following error:
C:\test_py>python ipv6.py
Traceback (most recent call last):
File "ipv6.py", line 119, in
main()
File "ipv6.py", line 113, in main
p = listenTCP6(, TrivialServerFactory())
File "ipv6.py", line 95, in listenTCP6
return reactor.listenWith(Port, port, factory,
backlog, interface)
File
"C:\python251\lib\site-packages\twisted\internet\posixbase.py",
line 499,
in listenWith
p.startListening()
File
"C:\python251\lib\site-packages\twisted\internet\tcp.py",
line 730, in st
artListening
skt = self.createInternetSocket()
File
"C:\python251\lib\site-packages\twisted\internet\tcp.py",
line 718, in cr
eateInternetSocket
s = base.BasePort.createInternetSocket(self)
File
"C:\python251\lib\site-packages\twisted\internet\base.py",
line 724, in c
reateInternetSocket
s = socket.socket(self.addressFamily,
self.socketType)
File "C:\Python251\lib\socket.py", line 154, in
__init__
_sock = _realsocket(family, type, proto)
TypeError: an integer is required
Thanks.
-
t0md
I run with the following command:
python ipv6.py
HERE IS THE CODE for ipv6.py:
#this is copied verbatim from
http://twistedmatrix.com/trac/browser/sandbox/exarkun/ipv6.py
#I'm unclear on the license that applies
import socket
from twisted.internet import tcp
from twisted.internet import protocol
from twisted.internet import reactor
class IPv6Address(object):
def __init__(self, type, host, port, flowInfo,
scope):
self.type = type
self.host = host
self.port = port
self.flowInfo = flowInfo
self.scope = scope
def __eq__(self, other):
if isinstance(other, IPv6Address):
a = (self.type, self.host, self.port,
self.flowInfo, self.scope)
b = (other.type, other.host, other.port,
other.flowInfo, other.scope)
return a == b
return False
def __str__(self):
return 'IPv6Address(%s, %r, %d, %d, %d)' % (
self.type, self.host, self.port,
self.flowInfo, self.scope)
def isIPv6Address(ip):
try:
socket.inet_pton(socket.AF_INET6, ip)
except:
return 0
return 1
class Client(tcp.Client):
addressFamily = socket.AF_INET6
def resolveAddress(self):
if isIPv6Address(self.addr[0]):
self._setRealAddress(self.addr[0])
else:
reactor.resolve(self.addr[0]).addCallbacks(
self._setRealAddress,
self.failIfNotConnected
)
def getHost(self):
return IPv6Address('TCP',
*self.socket.getsockname())
def getPeer(self):
return IPv6Address('TCP',
*self.socket.getpeername())
class Connector(tcp.Connector):
def _makeTransport(self):
return Client(self.host, self.port,
self.bindAddress, self, self.reactor)
def getDestination(self):
return IPv6Address('TCP', self.host,
self.port, 0, 0)
class Server(tcp.Server):
def getHost(self):
return IPv6Address('TCP',
*self.socket.getsockname())
def getPeer(self):
return IPv6Address('TCP', *self.client)
class Port(tcp.Port):
addressFamily = socket.AF_INET6
transport = Server
def _buildAddr(self, address):
return IPv6Address('TCP', *address)
def getHost(self):
return IPv6Address('TCP',
*self.socket.getsockname())
def getPeer(self):
return IPv6Address('TCP',
*self.socket.getpeername())
def connectTCP6(host, port, factory, timeout=30,
bindAddress=None, reactor=None):
if reactor is None:
from twisted.internet import reactor
return reactor.connectWith(
Connector, host, port, factory, timeout,
bindAddress
)
def listenTCP6(port, factory, backlog=5,
interface='::', reactor=None):
if reactor is None:
from twisted.internet import reactor
#IPV6_V6ONLY = 27, IPV6_BINDV6ONLY
#IPPROTO_IPV6 = 41
#_socket.socket.setsockopt(_socket.IPPROTO_IPV6,
_socket.IPV6_V6ONLY, 0)
return reactor.listenWith(Port, port, factory,
backlog, interface)
def main():
from twisted.internet import reactor
class TrivialProtocol(protocol.Protocol):
def connectionMade(self):
print 'I (', self.transport.getHost(), ')
am connected! (to ', self.transport.getPeer(), ')'