examples TCP-IP

examples TCP-IP:

TCP-IP Server + append value scales KG to file.log

TCP-IP Clients... send to Server  scales value KG.

# examples by skole www.pi+ppo.org

#UDP Communication

#!/usr/bin/env python

# Open a file for write

import socket

import sys

 

HOST = 'localhost'   # locahost

PORT = 5006 # port

 

# Datagram (udp) socket

try :

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    print 'Server UDP Socket created'

except socket.error, msg :

    print 'ERROR to create socket Code : ' + str(msg[0]) + ' Message ' + msg[1]

    sys.exit()

 

 

# Bind socket to local host and port

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print 'Bind ERROR Code : ' + str(msg[0]) + ' Message ' + msg[1]

    sys.exit()

     

print 'Socket bind complete'

 

#now keep talking with the client

while 1:

    # receive data from client (data, addr)

    d = s.recvfrom(1024)

    data = d[0]

    addr = d[1] 

    if not data:

        break

    reply = 'si OK...' + data

    s.sendto(reply , addr)

    print 'Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.strip()

     

s.close()


# examples by skole www.pi+ppo.org

#UDP Communication

#!/usr/bin/env python

# Open a file for write

 

import socket   #lib socket

import sys  #exit loop exit

 

# UDP socket

try:

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

except socket.error:

    print 'UDPClient ERROR to create socket'

    sys.exit()

 

host = 'localhost';

port = 5006;

 

while(1) :

    msg = raw_input('UDP Client Enter message to send : ')

    try :

        s.sendto(msg, (host, port))

        d = s.recvfrom(1024)

        reply = d[0]

        addr = d[1]

        print 'Server UDP reply : ' + reply

     

    except socket.error, msg:

        print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]

        sys.exit()


sample... ACT buttons

application for the automatic creation of buttons

to execute commands BASH or applications to run in the

background: bg and / or &> $ HOME / nohup.out


# examples by skole www.pi+ppo.org

#UDP Communication

#!/usr/bin/env python

import Tkinter

from Tkinter import *

import socket

import sys

import time

import os


ActServer='/home/skole/pythonTCP-IP/UDPServer.py'

ActClient='/home/skole/pythonTCP-IP/UDPClient.py'

Actlslh='ls -lh *.py'

Actdf='df'


def ACT(id, tex):

return lambda : pyscripts(id, tex)



def pyscripts(id, tex):

    s = 'ACT {} command {}\n'.format(id, id*1)

    #tex.insert(Tkinter.END, s)

    #tex.see(Tkinter.END)             # Scroll if necessary #

    if id==1:

tex.insert(Tkinter.END, 'server UDP %s act...\n' %ActServer)

os.system("python %s &" %ActServer)

    if id==2:

tex.insert(Tkinter.END, 'client UDP %s act...\n' %ActClient)

os.system("python %s " %ActClient)

    if id==3:

tex.insert(Tkinter.END, 'sh %s act...\n' %Actlslh)

os.system("%s &" %Actlslh)

    if id==4:

tex.insert(Tkinter.END, 'sh %s act...\n' %Actdf)

os.system("%s &" %Actdf)



MyWindow = Tkinter.Tk(className="MyWindow")

tex = Tkinter.Text(master=MyWindow)

tex.pack(side=Tkinter.RIGHT)

bop = Tkinter.Frame()

bop.pack(side=Tkinter.LEFT)

for idbutton in range(1,8):

if idbutton == 1:

stringAct = 'ACT UDPServer {}'.format(idbutton)

elif idbutton == 2:

stringAct = 'ACT UDPClient {}'.format(idbutton)

elif idbutton == 3:

stringAct = 'ACT %s {}'.format(idbutton) %Actlslh

elif idbutton == 4:

stringAct = 'ACT %s {}'.format(idbutton) %Actdf

else: stringAct = 'ACT ...{}'.format(idbutton)

b = Tkinter.Button(bop, text=stringAct, command=ACT(idbutton, tex))

b.pack()

   

Tkinter.Button(bop, text='Exit', command=MyWindow.destroy).pack()

MyWindow.mainloop()


CONTACT US

 

  Tel: .....

FOLLOW US

PROFILE

Profile ...

© Copyright 2016-2019 All Rights Reserved.