vendredi 6 mars 2020

How to send data via python code to the website?

I have the python code to get socket data rcv from other client.Is it possible to print data rcv on the website?Should i change port to 80?

#SERVER.py

import errno
import os
import socket
import time
import threading
import RPi.GPIO as GPIO

host = "192.168.10.95"
port = 12345

hostName    = socket.gethostname()
ipAddress   = socket.gethostbyname(hostName + ".local")
print(hostName)
print(ipAddress,':',port)

def accept_req(client,addr):
    i = 0
    print ('Got connection from',addr)

    while True:

        try:
            i += 1
            buf = "%s:%d" %(hostName,i)
            buf = bytes(buf,'utf-8')
            client.send(buf)
            rcv = client.recv(1024).decode('utf-8')
            if not rcv:
                print("there is no rcv")
                client.close()
                print("closed")
                break
            print(rcv)
        except socket.error as e:
            print(os.strerror(e.errno))
            client.close()
            print("closed")
            break

        time.sleep(1)


s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host, port))
s.listen(0)

while True:
    c, addr = s.accept()
    task = threading.Thread(target = accept_req,args = (c,addr))
    task.start()
<!--web app to recieve data from python-->
<html>
<head>
    <title>server</title>
</head>

<body>
    <font size=10><center>
    <B>Server</B>
    <p></p>
    </center>
    </font>
    <script>    
        a = document.getElementsByTagName("p");     
        setInterval(function(){ 
            a[0].innerHTML = "print socket data here";
        },1000);        
    </script>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire