Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Python - how do I - array of IPAddresses?

Posted on 2014-09-25
3
375 Views
Last Modified: 2014-09-30
Hi
Thanks for getting me this far in my server code.

My clients are all joining, but I need the best way of storing client information for later use.
I basically need an array of structs for the clients, like
struct client {
ipaddress address;
int PORT;
}
Their index in the array of client information would correspond with their client number.
I'm considering having an array of addresses and an array of PORTS, - should that be enough info for the clients?
maybe, also a boolean if they are still connected?

What is the code for declaring an array of ipaddresses?
or, how do I declare an array of tuples? structs?

Thanks
0
Comment
Question by:beavoid
  • 2
3 Comments
 

Author Comment

by:beavoid
ID: 40345239
I can make an array of bytes

bytearray(100)
will give you 100 zero bytes.

How does it work for complex data?
0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 40345599
If you just want to keep track of this information and if you don't have to store the data in a memory optimized way:


Why don't you just make a list of objects.
or a dict of objects (acting as a sparse array)

Is there any erason you want to declare that array.
WOuldn't it be enough to add new client info whenever you have new data.

class ClientInfo():
    def __init__(self, ip=None, port=None):
        self.ip = ip
        self.port=port

clients = []




to create a new client you would just write
clients.append(ClientInfo('127.0.0.1', 8080)

# next line of you want to keep the index of the client
client_idx = len(clients)-1

please give me more context if I misunderstood your question
0
 

Author Comment

by:beavoid
ID: 40346626
Thanks
I would just like to store the client info once the clients in the game arrive.

If the server expects n clients, the clients may, will arrive out of order.

Last time I did an RTS, in Java, I declared an array of clientInfo objects, because I knew how many to expect.
- And I assign a client a clientNumber corresponding to the order in which they joined?
So the ClientInfo code above goes in the server .py  - even tho it is a class? and the server class code after it?

Wouldn't I also need a ClientNumber parameter? clientnumber?
ClientNumber = clientnumber
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 166
Is it true to say that Python is successor of Perl? 7 189
bunnyEars2 challenge 6 146
Difference of import usage in Python 1 75
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question