Bar K
Bar K

Reputation: 89

Set Up a Simple Server in Python

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?

Upvotes: 0

Views: 164

Answers (1)

Steven Mercatante
Steven Mercatante

Reputation: 25315

You're not actually calling server().

Add this after the function definition:

if __name__ == '__main__':
  server()

Upvotes: 3

Related Questions