Forums

How to prevent python socket server from sending blank message?

I've set up a basic socket server/client script - hope to somehow implement it in website, and have seen that it's possible to do app to app?

anyway, can't seem to prevent user from crashing the server or client if they enter a blank message (ie. hit return without entering any text/data). Is there an easy way to prevent this?

server.py

def server_program():
    # get the hostname
    host = socket.gethostname()
    port = 8000  # initiate port no above 1024

    server_socket = socket.socket()  # get instance
    # look closely. The bind() function takes a tuple as argument
    server_socket.bind((host, port))  # bind host address and port together

    # configure how many clients the server can listen to simultaneously
    server_socket.listen(2)
    conn, address = server_socket.accept()  # accept new connection
    print("Connection from: " + str(address) + " Now wait for message")
    while True:
        # receive data stream. it won't accept data packet greater than 1024 bytes
        data = conn.recv(1024).decode()
        #conn.sendall(data)
        if not data:
            # if data is not received break
            break
        print("from Client: " + str(data))
        data = input('Enter message:-> ')
        conn.send(data.encode())  # send data to the client
        print('message sent!')

    conn.close()  # close the connection


if __name__ == '__main__':
    server_program()

client py:

import socket

def client_program(): host = socket.gethostname() # as both code is running on same pc port = 8000 # socket server port number

client_socket = socket.socket()  # instantiate
client_socket.connect((host, port))  # connect to the server

message = input(" Enter message:-> ")  # take input

while message.lower().strip() != 'bye':
    client_socket.send(message.encode())  # send message
    data = client_socket.recv(1024).decode()  # receive response

    print('Received from SERVER: ' + data)  # show in terminal

    message = input("Please send a message:-> ")  # again take input

client_socket.close()  # close the connection

if name == 'main': client_program()

this works fine (have added: import socket to top line of server.py) - if users behave correctly but if one person hits return at the wrong time, or tries sending two message then the whole thing just hangs

So I want to fix that and then somehow add it to my web page/app - can that be done?

Don't just send the message. Check that it is not blank and then decide if it's a message that you wanted to send or if it's just a return.

I'm not usre I understand what you mean by sending 2 messages at the same time. I don't see anything in the code that would allow simultaneous sending of 2 messages.

I've tried various methods for that , for ex I've inserted:

if data == None
conn.send(data.encode()

if data = False

while data != ' '

etc... all sorts of combinations and none of them do the trick

what am I doing wrong here?

tried this just now:

server.py

while True:
            data = input('Enter message:-> ')
        if data != ' ':
            conn.send(data.encode())  # send data to the client
            print('message sent!')
        else:
            print('cannot send blank message!')

    conn.close()  # close the connection

client.py

 if data == None:
            print('Recieved blank message!')  # show in terminal
        else:
            print('Received from server: ' + data)  # show in terminal

        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection

this doesn't really sort it

Are you sure that data is None when the user doesn't enter a message? Also, you're not changing anything - you're still running the code that sends the message when data is None (assuming it ever is).

Your first attempt looks like it may work if data is an empty string when the user just hits return.