0

I have recently starting learning network programming with python. Here is a simple server I tried to write:

import socket

def server():
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('127.0.0.1', 1024))
    while True:
        data, address = sock.recvfrom(65535)
        text = data.decode('ascii')
        print('the client from {0} sent: "{1}"'.format(address, text))
        if text is '0': break

I wanted the server to wait till it is getting packets from the server, but when I run it, it will close instantly. what did I do wrong?

3

1 Answer 1

3

You're not actually calling server().

Add this after the function definition:

if __name__ == '__main__':
  server()
Sign up to request clarification or add additional context in comments.

1 Comment

@BarK You may check this Answer as answering your question so we all stop spending time reviewing it over and over ? ;-) No need to be ashamed BTW you're learning, we're all still learning, even 20 years later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.