внешний
Не касаясь вашего кода Python:
bash
hello.py >/dev/tcp/$host/$port # if tcp
hello.py >/dev/udp/$host/$port # if udp
netcat
hello.py | nc $host $port # if tcp
hello.py | nc -u $host $port # if udp
внутренний
Использованиемодуль розетки.
import socket
sock= socket.socket(socket.AF_INET, socket.SOCK_STREAM) # if tcp
sock= socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # if udp
# if it's TCP, connect and use a file-like object
sock.connect( (host, port) )
fobj= sock.makefile("w")
# use fobj and its write methods, or set sys.stderr or sys.stdout to it
# if it's UDP, make a dummy class
class FileLike(object):
def __init__(self, asocket, host, port):
self.address= host, port
self.socket= asocket
def write(self, data):
self.socket.sendto(data, self.address)
fobj= FileLike(sock, host, port)