[Tutor] creating a server

nathan tech nathan-tech at hotmail.com
Wed Oct 7 12:41:21 EDT 2020


hi folks,

I'm not sure if this is the write mailing list to ask this question, as 
it's not really exactly a newbie question.

Well, I guess it is but... Anyway.


I'm going to build  server in python and wondered if someone would be 
willing to spend just a second to answer these questions so I can 
understand:


I see my server having somewhat of this structure:


connections=[]

shutdown=False

while shutdown != True:

  check_for_connections() # this would handle any connections that were 
received on socket and if some were found, append them to the 
connections variable

  for x in connections:

   data=x.recv(1024)

   if(data):

    # act upon this data


# server is shutting down, so send a goodbye message to the connections 
and then close.


My questions are simple really:

1. Is this a basic good starting point?

2. Is there some sort of way of saying: if(sock.still_connected())?

3. Is recv(1024) the right way to go here, or is there a better way of 
ensuring all the relevant data gets through/

4. I'd like to have it event based, what I mean is it would be pretty 
awesome if for instance the client sent an object like this one:


class evt:

  def __init__(self, type):

   self.type=type

   self.message=None

  Then something like:

e=evt("new_message")

e.message="Hi! I'm sending you a very cool message!"

sock.send(e)


I've experimented a little bit with something like:


data=sock.recv(1024)

if(data and data[:-3]=="|||"):

  # all data received

  data=data.strip("|||")

  data=data.split("|")

  type=data[0]

  message=data[1]


But this is obviously easily broken because of just throwing a | in 
there to break it all.


Finally, I am going to be doing some research on this so please feel 
free to skip this final, but what is the advantage of twist over the 
standard socket library?

Thanks in advance and, if this is truly the wrong place, I can only ask 
for direction.

nathan




More information about the Tutor mailing list