Python Forum
socket.gaierror: [Errno -2] Name or service not known
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.gaierror: [Errno -2] Name or service not known
#6
Before you send, text must be converted to bytes or you have already raw-bytes.
If you have a text, it could be ASCII, utf8, latin1 or other encodings.
Python uses as standard utf8 as encoding for source code and all other stuff.
ASCII is a subset of Unicode, so no worry about that.

By the way, copying scripts leads into poor code quality.
A little bit improved, using the modern features of Python 3.6.
No external dependencies. Hint: If you want to make good command line applications, use click.

#!/usr/bin/env python3

"""
This program connects to a ip and port via udp or tcp
and sends the datafiles to the destination.
"""

import socket
import sys
from pathlib import Path


class ArgumentError(Exception):
    pass


def usage():
    exe = Path(sys.executable).name
    prg = sys.argv[0]
    descr = __doc__
    cmd_usage = f'Usage: {exe} {prg} <ip> <port> <udp|tcp> <datafile> <...>'
    print(descr)
    print()
    print(cmd_usage)


def get_payloads(datafiles):
    return [Path(file).read_bytes() for file in datafiles]


def get_arguments():
    if len(sys.argv) < 5:
        usage()
        raise ArgumentError('Wrong number of arguments')
    ip, port, proto, *datafiles = sys.argv[1:]
    if proto == 'udp':
        sock_args = (socket.AF_INET, socket.SOCK_DGRAM)
    elif proto == 'tcp':
        sock_args = (socket.AF_INET, socket.SOCK_STREAM)
    else:
        usage()
        raise ArgumentError(f'Wrong protocol: {proto}')
    try:
        addr = (ip, int(port))
    except ValueError:
        usage()
        raise ArgumentError('Port must be a valid number')
    return addr, proto, sock_args, datafiles


def send_payloads_udp(connection, dest, payloads):
    for n, payload in enumerate(payloads, start=1):
        connection.sendto(payload, dest)
        print(f'[*] Payload {n} sent')


def send_payloads_tcp(connection, payloads):
    for n, payload in enumerate(payloads, start=1):
        connection.sendall(payload)
        print(f'[*] Payload {n} sent')


def main():
    try:
        addr, proto, sock_args, datafiles = get_arguments()
    except ArgumentError as e:
        print('Error:', e)
        return 1
    try:
        payloads = get_payloads(datafiles)
    except Exception as e:
        print('Error:', e)
        return 10
    with socket.socket(*sock_args) as sock:
        if proto == 'tcp':
            try:
                sock.connect(addr)
            except Exception as e:
                print('Error:', e)
                return 2
            print("[*] Connected")
            try:
                send_payloads_tcp(sock, payloads)
            except Exception as e:
                print('Error:', e)
                return 20
        elif proto == 'udp':
            try:
                send_payloads_udp(sock, addr, payloads)
            except Exception as e:
                print('Error:', e)
                return 10


if __name__ == '__main__':
    retval = main()
    sys.exit(retval)
Output:
deadeye@nexus ~ $ python payload_sender.py This program connects to a ip and port via udp or tcp and sends the datafiles to the destination. Usage: python payload_sender.py <ip> <port> <udp|tcp> <datafile> <...> Error: Wrong number of arguments
* The command python points on my machine to a local Python 3.7.3 installation.

Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 tcp test.py test_speed.py Error: [Errno 111] Connection refused
Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 tcp test.py test_speed.py [*] Connected [*] Payload 1 sent [*] Payload 2 sent
Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 udp test.py test_speed.py [*] Payload 1 sent [*] Payload 2 sent
Now you can send much garbage to public services, which is also in some countries prohibited by laws.
I you live in Germany, don't do this. If you want to be a nice internet user, don't send garbage to public services.

But the same task can be done also with netcat.
More interesting is to build own IP Headers, TCP Headers + Payload.
What the code totally misses, is receiving data. So you don't see the servers reply to messages.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: socket.gaierror: [Errno -2] Name or service not known - by DeaD_EyE - May-25-2019, 02:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.gaierror: [Errno 11001] Johnygo 4 10,800 Nov-03-2019, 08:52 PM
Last Post: nemat
  socket.gaierror: [Errno -2] Name or Service not known ItsAGuyaneseTing 1 11,668 Mar-02-2018, 07:14 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020