I want to imitate a DNS, like nslookup in python with UDP sockets. How should I build up the request?
import socket
UDP_IP = '8.8.8.8'
UDP_PORT = 53
dn = 'www.google.com'
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
msg = '???'
sock.sendto(msg, (UDP_IP, UDP_PORT))
(data, addr) = sock.recvfrom(1024)
print data, addr
Related
I'm trying to implement a loop for this socket code, but I can't quite seem to wrap my head around it. Is anyone able to explain it for me?
Here's the code
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Do you possible mean this?
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
I've got a TCP socket: the simplest client-server application.
First question: How can I generate a self-signed and non-self-signed SSL packet in Python?
Second question: How can I transfer SSL packets from the client to the server?
Programming language is python.
Server Code:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Client side code:
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
I made a little chat program, and I'm using in to chat with a friend over WAN.
We are now worried about security: our program just uses socket module to send byte encoded strings.
How would this appear to an interceptor?
How could I use a SSL connection between us two to make our messaging secured?
Will SSL be enough to make our chat 100% private?
Thank you
Also, here is our chat's code:
Server-side:
import socket
HOST = my ip here
PORT = 1234
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORTA))
print('Listening')
s.listen()
conn, addr = s.accept()
with conn:
print('Connected to', addr)
conn.sendall(b'')
while True:
data = conn.recv(1024).decode('utf-8')
print(data)
risp = input('Message: ').encode('utf-8')
conn.sendall(risp)
if not data:
print('Not receiving')
break
client side:
import socket
HOST = 'my ip' # The server's hostname or IP address
PORT = 1234 # The port used by the server
something = ''
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while something!= 'esc'.encode('utf-8'):
something= input('Your messagge:').encode('utf-8')
s.sendall(something)
data = s.recv(1024)
print(data.decode('utf-8'))
print('Received: ', repr(data))
listenr code :
import socket
host = socket.gethostbyname(socket.gethostname())
port = int(raw_input("PORT > "))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(5)
while True:
c, addr = server.accept()
buff = 2048
print addr[0]+" connected."
c.send("Connection Established")
data = c.recv(buff)
if data:
print data
client code:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = int(raw_input("PORT > "))
server.connect((host, port))
buff = 2048
data = server.recv(buff)
if data:
print data
and is it possible to receive data from client and listen on port at the same time ? how?
After accept() use thread to send/receive data to/from client and at the same time main thread can wait for next client running accept() again. It is standard method .
I'm using the Python socket library to send a message to another server and measure the roundtrip latency. I'm new networks so it's very basic, just using the send-receive examples given in the socket docs (http://wiki.python.org/moin/UdpCommunication). For the moment I'm using time.time() to timestamp when the message goes out and when it comes back, though I want to be able to get nanosecond accuracy in the end. Right now, I get about 300microseconds through my python script but pinging from the shell yields about 100micros. What can I do to get faster communication and/or more accurate measurement?
Script that sends one message:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
print "SENT {0}".format(int(time.time()*1000*1000)
This is the script that listens for the ping to come back
import socket
UDP_IP = "originating side localhost ip"
UDP_PORT = 9112
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
print "received message: {0} {1}".format(data, int(time.time()*1000*1000)
This is the script that listens on the server being pinged. When it receives a message on the port, it sends a message right back
import socket
UDP_IP = "pinged server's localhost ip"
UDP_PORT = 9111
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
UDP_IP2 = "ip of server that sent ping"
UDP_PORT2 = 9112
MESSAGE = "HEY"
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Listening on PORT 9111"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
sock.sendto(MESSAGE, (UDP_IP2, UDP_PORT2))
print "received message: ", data