Thursday, December 15, 2016

Python 3 a few network scripts

Get netmask

import socket
import fcntl
import struct

iface='eth0'
socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', iface.encode('utf-8')))[20:24])

A broadcast server
from socket import *
import time

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while True:
    s.sendto('this is testing'.encode('utf-8'), ('255.255.255.255', 12345))
    time.sleep(1)



A broadcast listener
from socket import *
s=socket(AF_INET, SOCK_DGRAM)
s.bind(('', 12345))
while True:
    m=s.recvfrom(1024)
    print(m[0])

No comments:

Post a Comment