简体   繁体   中英

python client server application

I accessed raspberry pi on my pc(windows 10). The server is running on raspberry pi and I want to send data to the server from my pc(client running on my PC, not in raspberry pi).

client.py

import socket
mysoc=socket.socket()
mysoc.connect(("xxx.xxx.xxx.xxx",1234))     //ip address of raspberrypi
while 1:
    s=input("Enter command")
    mysoc.sendall(s.encode())
    if s==exit:
        break
mysoc.close()

server.py

import socket
from gpiozero import LED
led=LED(17)

server_soc=socket.socket()
server_soc.bind(("",1234))
server_soc.listen(5)
print("Listening to client ...")
conn,addr=server_soc.accept()
print("connected to client:",addr)
while 1:
        data=conn.recv(1024)
        print(data)
        if data==b"on\n":
            print("LED is on")
            led.on()
        elif data==b"off\n":
            print("LED is off")
            led.off()
        else:
            break       
conn.close()
server_soc.close()

I get the following error when client.py is executed.

>>> 
= RESTART: C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\My_Programs\client.py
Enter commandon
Enter commandoff
Enter commandon
Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\My_Programs\client.py", line 7, in <module>
    mysoc.sendall(s.encode())
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
>>>

server.py

>>> %Run server_control_led.py
Listening to client ...
connected to client: ('xxx.xxx.xxx.x', 50603)   //ip address of my pc
b'on'
>>> 

The server receives the first data and stops listening.
I tried to disable antivirus and firewall but the issue still exists.
I unable to figure the root cause of this closing connection. whether issue is on the server side or client-side.
I am using putty and xming server and python code. I tried doing some changes in inbound and outbound rules in the firewall but still doesn't work.
what should be configured?

You should specify the type of encoing in your client's 6th line. Instead of mysoc.sendall(s.encode()) you should use mysoc.sendall(s.encode('utf-8')) . If you don't want to use utf-8 you can always pass other encoding methods such as ascii. -Update. There's another error in the 7th line, where you try to compare s with a string but forget to put the quotes: Your code: if s==exit: Correction: if s=="exit":

-Update #2: I've just found the actual error: you don't decode the string properly on the server side, so the conditional doesn't identify the command as 'on' or 'off' but as something else, that's why the server exits after receiving the first message. Here's the fixed code: (Server)

import socket
server_soc=socket.socket()
server_soc.bind(("",1234))
server_soc.listen(5)
print("Listening to client ...")
conn,addr=server_soc.accept()
print("connected to client:",addr)
while 1:
        data=conn.recv(1024)
        print(data)
        data=data.decode('utf-8')
        if data=="on":
            print("LED is on")
        elif data=="off":
            print("LED is off")
        else:
            break       
conn.close()
server_soc.close()

(Client)

import socket
mysoc=socket.socket()
mysoc.connect(("127.0.0.1",1234))
s=""
while s!="exit":
    s=input("Enter command")
    mysoc.sendall(s.encode('utf-8'))
mysoc.close()

(I deleted all of the commands related to LED so as to be able to run the scripts in my computer, you'll have to add them to the code again).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM