Alina Khachatrian
Alina Khachatrian

Reputation: 757

UDP TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

I'm completely newbie to python and computer networking. While working on Uni project I have faced a problem. What am I doing wrong? Any help will me much appreciated.

Here is server side:

import socket

def Main():
    host = "127.0.0.1"
    port = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    print ("Server Started.")
    while True:
        data, addr = s.recvfrom(1024)
        print ("message from: ") + str(addr)
        print ("from connected user: ") + str(data.decode('utf-8'))
        data = str(data).upper()
        print ("sending: ") + str(data)
        s.sendto(data, addr)

    s.close()

if __name__ == '__main__':
    Main()

Here is my client side:

import socket

def Main():
    host = "127.0.0.1"
    port = 5000

    server = ('127.0.0.1', 5000)

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    message = input('->')
    while message != 'q':
        s.sendto(message.encode('utf-8'), server)
        data, addr = s.recvfrom(1024)
        print ('Received from server: ') + str(data)
        message = input('->')
    s.close()

if __name__ == '__main__' : 
    Main()

Upvotes: 0

Views: 236

Answers (1)

initialed85
initialed85

Reputation: 184

There were a couple of issues; mostly with the printing.

You had a few instances of print('some text') + str(data); this won't work, because while print() outputs to the screen (STDOUT) it returns None, so what you were actually doing was trying to concatenate None + str(data)

What you need is print('some text' + str(data)).

Additionally, there was as issue on the server-side where you echo the data received from the client back to the client- it needed to be re-encoded as a bytearray (it comes in as a bytearray, gets converted to a utf-8 string for display, it needs to go back to bytearray before replying).

In summary, server:

import socket


def Main():
    host = "127.0.0.1"
    port = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    print("Server Started.")
    while True:
        try:
            data, addr = s.recvfrom(1024)
            print("message from: " + str(addr))  # moved string concatenation inside print method
            print("from connected user: " + str(data.decode('utf-8')))  # moved string concatenation inside print method
            data = str(data).upper()
            print("sending: " + str(data))  # moved string concatenation inside print method
            s.sendto(data.encode('utf-8'), addr)  # needed to re-encode data into bytearray before sending
        except KeyboardInterrupt:  # added for clean CTRL + C exiting
            print('Quitting')
            break

    s.close()


if __name__ == '__main__':
    Main()

And the client:

import socket


def Main():
    host = "127.0.0.1"
    port = 5001

    server = ('127.0.0.1', 5000)

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    message = input('->')
    while message != 'q':
        try:
            s.sendto(message.encode('utf-8'), server)
            data, addr = s.recvfrom(1024)
            print('Received from server: ' + str(data))  # moved string concatenation inside print method
            message = input('->')
        except KeyboardInterrupt:  # added for clean CTRL + C exiting
            print('Quitting')
            break

    s.close()


if __name__ == '__main__':
    Main()

Upvotes: 1

Related Questions