python udp server parse and send received data to MySQL - python

i am working on some proof of concept study research project and have python udp socket server that listen received data.
Client send data NAME and FAMILY NAME on UDP to server.
I would like to receive that data on UDP socket server side and on receive send this data to mysql database with two fields f_name and l_name.
import socket
UDP_IP = "192.168.1.10"
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
print "UDP SERVER STARTED!"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "MSG Received:", data
this example is taken from web and with this server i get data on console.
I would like to have it like below and of course code/concept can be changed. This might be solved with scapy sniffer but that would be dirty.
Conceptually i would like to have ti something like:
1. socekt server received data
2. parse data received and send this data to mysql
I started with this in mind but doesnt work
import socket
import MySQLdb
UDP_IP = "192.168.1.10"
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
print "UDP SERVER STARTED!"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
def parse(data):
db = MySQLdb.connect("localhost","db_user","db_pass","directory_db")
cursor = db.cursor()
# Params to insert into DB
f_nameObj = re.search(r'NAME: (.*?) .*', data, re.M|re.I)
if f_name:
f_name = f_nameObj.group(1)
l_nameObj = re.search(r'SURNAME: (.*?) .*', data, re.M|re.I)
if l_name:
l_name = l_nameObj.group(1)
# MySQL EXECUTION
cursor.execute("""
INSERT INTO dictionary (f_name, l_name) VALUES (%s, %s)""",(f_name,l_name))
#
db.commit()
With this kind of udp server i see no messages so seems function that parse data is not working with server.
Any help or guidance would be appreciated

In your server when you receive data you don't call parse function.You just print the content of data. Add the line with the comment and see the result.
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
parse(data) # Call the Function

Related

View raw packet-level data using python sockets

Is it possible to view the raw socket data that is sent to a server via the sockets module? For example, the following will print the client request in bytes:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 1))
s.listen(1)
conn, addr = s.accept()
with conn:
while True:
data = conn.recv(200)
print ("INCOMING BYTES:", data) # how to view the actual sent TCP packet data ?
if not data: break
conn.sendall(data)

How do you send a dictionary over a socket in Python?

I know that similar questions have been raised but they don't seem to work for me! I have tried serializing the dictionary then converting that to a string then encoding it before I send it over the socket. No success so far!
This is my server code:
#library
import socket
import pickle
#socket initialization
host = "127.0.0.1"
port = 5000
mainAddr = (host, port)
#dict initialization
dataDict = {} #just imagine that dict has content
#create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
s.bind((mainAddr))
s.listen(4)
print('program started')
print('listening..')
while True:
try:
conn, addr = s.accept()
print("connection from: "+str(addr))
print("sending message..")
pickle.dumps(dataDict)
print('pickled!')
dataS = str(dataP)
print('stringed!')
dataE = dataS.encode('UTF-8')
print('encoded!')
s.sendto(dataE,addr)
print('data sent!')
except:
pass
s.close()
For the socket initialization, I've tried other types:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
s = socket.socket()
For the sending part, I've tried these alternatives:
s.send(dataE)
s.send(dataE,addr)
s.sendall(dataE)
s.sendall(dataE,addr)
When I run the program, these get printed out:
program started
listening..
connection from:<insert addr here>
sending message..
pickled!
stringed!
encoded!
Only data sent! is not sent. So I am guessing that it's the sending part that has a problem.
For the client side, here's the code:
#library
import socket
import pickle
#initialization
host = '127.0.0.1'
port = 5000
buffer = 1024
#create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
s.connect((host,port))
print('connected!')
#receive dictionary
print('receiving message..')
while True:
data, addr = s.recvfrom(buffer)
print('received!')
dataD = data.decode("UTF-8")
print('decoded!')
dataP = pickle.loads(dataD)
print('unpickled!')
print(str(dataP))
s.close()
In the client terminal, only the following prints:
connected!
receiving message..
On the client side, I've tried changing the order of unpickling and decoding but still, to no avail.
A TCP server socket is not actually used for sending/receiving data; I'm surprised you're not getting an error when calling s.send() or similar on it. Instead, it's a factory for producing individual sockets for each client that connects to the server - conn, in your code. So, conn.sendall() is what you should be using. No address parameter is required, the individual socket already knows who it is talking to. (.send() is unreliable without some extra work on your part; .sendto() is only used with UDP sockets that have not been connected to a particular client.)

python video streaming display packets

I am working on a Networks course project, in which I have to create a video streaming server. I found this link for a simple python client/server socket binary stream that seems quite useful. I am able to send video files as packets over the network, but the receiving side is saving the incoming packets as a file. I would like to display the incoming packets as a video stream (preferably on a web browser using HTML), instead of writing to a file. Please suggest some possible method of doing this. Thanks.
As I am doing a project, I would like to create a streaming server from scratch rather than use existing implementations like Flumotion.
Here's the code for the sending and receiving sides:
Sending side:
import socket
HOST = 'localhost'
PORT = 9876
ADDR = (HOST,PORT)
BUFSIZE = 4096
videofile = "./test2.mp4"
bytes = open(videofile).read()
print len(bytes)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
client.send(bytes)
client.close()
Receiving side:
import socket
HOST = ''
PORT = 9876
ADDR = (HOST,PORT)
BUFSIZE = 4096
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(ADDR)
serv.listen(5)
print 'listening ...'
while True:
conn, addr = serv.accept()
print 'client connected ... ', addr
myfile = open('testfile.mov', 'w')
while True:
data = conn.recv(BUFSIZE)
if not data: break
myfile.write(data)
print 'writing file ....'
myfile.close()
print 'finished writing file'
conn.close()
print 'client disconnected'

Python How to build a *real* ip address multi-client server

let's say I have a multi-client server socket and a client socket in python.
Server: (You don't have to read all of the server's code, just know it's a multi-client server.
import socket, select
CONNECTION_LIST = [] # list of socket clients
RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 5000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
RealServerIP = ? # I want to have a real server ip which would let me connect to the server from any computer around the globe...
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((RealServerIP, PORT))
server_socket.listen(10)
# Add server socket to the list of readable connections
CONNECTION_LIST.append(server_socket)
print "Chat server started on port " + str(PORT)
while 1:
# Get the list sockets which are ready to be read through select
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
for sock in read_sockets:
#New connection
if sock == server_socket:
# Handle the case in which there is a new connection recieved through server_socket
sockfd, addr = server_socket.accept()
CONNECTION_LIST.append(sockfd)
print "Client (%s, %s) connected" % addr
#Some incoming message from a client
else:
# Data recieved from client, process it
try:
#In Windows, sometimes when a TCP program closes abruptly,
# a "Connection reset by peer" exception will be thrown
data = sock.recv(RECV_BUFFER)
# echo back the client message
if data:
sock.send(data)
# client disconnected, so remove from socket list
except:
broadcast_data(sock, "Client (%s, %s) is offline" % addr)
print "Client (%s, %s) is offline" % addr
sock.close()
CONNECTION_LIST.remove(sock)
continue
server_socket.close()
(example from http://www.binarytides.com/code-chat-application-server-client-sockets-python/).
And 3 clients which are the most simple clients you can imagine to yourself:
import socket # imports module named 'socket'
RealServerIP = ? # I need your help here.... read the continuation
my_socket = socket.socket() # creates new socket named 'my_socket'
my_socket.connect((RealServerIP, 5000)) # connects to the server
my_socket.send(str) # sends string to the server
data = my_socket.recv(1024)
print data # prints data
my_socket.close()
I wanted to check whether my server can comunicate with those 3 clients at the same time. So I want to make my server as a public server like a web-server of Facebook, etc.
So any computer around the world could connect to it.
So, I tried to figure out how would I store my python server socket online with a specific IP and PORT which has nothing to do with my localhost, I want it to be real! Like any chat/web server you know..
If you mean the IP which the user connect with it to the internet, I don't think there is a way to get it by socket module, But i got a way to do the same thing using requests module if this may help.
import requests
def get_ip():
url = 'https://www.cmyip.com'
user_ip = requests.get(url).content.decode("utf-8").split("My IP Address is ")[1].split(" <a class=")[0]
return user_ip
I hope this help.
Note: This was tested on python 3.4.3 and it is working fine. I don't know about 2.X versions.

Setting up UDP client (python)

I tried to set up a UDP client and see if I can print the data like below:
import socket
target_host = "127.0.0.1"
target_port = 80
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#send some data
client.sendto("AAABBBCCC", (target_host, target_port))
#receive some data
data, addr = client.recvfrom(4096)
print data
So I execute it with
$python udp_client.py
But then the shell just stuck at this line in the code
data, addr = client.recvfrom(4096)
How to solve this problem?

Categories