Skip to content

Commit

Permalink
Merge pull request #80 from 5IGI0/master
Browse files Browse the repository at this point in the history
add IPv6 support
  • Loading branch information
kevinkjt2000 authored Apr 9, 2020
2 parents db5b816 + 65d7c28 commit b244c4e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
3.1
* Added IPv6 support

3.0
* Drop support for python 2 and python 3.4; you may use version 2.3 or prior for outdated python versions.

Expand Down
3 changes: 2 additions & 1 deletion mcstatus/protocol/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import socket
import struct

from ..scripts.address_tools import ip_type

class Connection:
def __init__(self):
Expand Down Expand Up @@ -160,7 +161,7 @@ class UDPSocketConnection(Connection):
def __init__(self, addr, timeout=3):
Connection.__init__(self)
self.addr = addr
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket = socket.socket(socket.AF_INET if ip_type(addr[0]) == 4 else socket.AF_INET6, socket.SOCK_DGRAM)
self.socket.settimeout(timeout)

def flush(self):
Expand Down
15 changes: 15 additions & 0 deletions mcstatus/scripts/address_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import socket
from urllib.parse import urlparse
from ipaddress import ip_address

def ip_type(address):
try:
return ip_address(address).version
except ValueError:
return None

def parse_address(address):
tmp = urlparse("//"+address)
if not tmp.hostname:
raise ValueError("Invalid address '%s'" % address)
return (tmp.hostname, tmp.port)
10 changes: 2 additions & 8 deletions mcstatus/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mcstatus.pinger import ServerPinger
from mcstatus.protocol.connection import TCPSocketConnection, UDPSocketConnection
from mcstatus.querier import ServerQuerier
from mcstatus.scripts.address_tools import parse_address
import dns.resolver


Expand All @@ -11,14 +12,7 @@ def __init__(self, host, port=25565):

@staticmethod
def lookup(address):
host = address
port = None
if ":" in address:
parts = address.split(":")
if len(parts) > 2:
raise ValueError("Invalid address '%s'" % address)
host = parts[0]
port = int(parts[1])
host, port = parse_address(address)
if port is None:
port = 25565
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='mcstatus',
version='3.0.0',
version='3.1.0',
author='Nathan Adams',
author_email='[email protected]',
url='https://pypi.python.org/pypi/mcstatus',
Expand Down

0 comments on commit b244c4e

Please sign in to comment.