2018-12-25 15:33:46

Hi guys.
Tell me about working with the network in Python.
What libraries, maybe you have examples?
Thanks in advance!

2018-12-26 01:46:58

There are a few ways to do it, [Sockets], also [Asyncio] in python 3, or if your looking for a library there's [Twisted Python], or if your more into web applications theres [Django].

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2018-12-26 19:35:24

magurp244, I heard that for sockets, you need to know multithreading in Python. It's true?

2018-12-26 19:43:08

@3, uh... no? You don't. I wrote a port scanner that uses no threads at all (though I wanted it to work in parallel, it failed). That was using good ol classic Python sockets, which was perfect for the job (anything else would've been overkill). For anything truly serious, multithreading is a given, no matter what your using.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-26 20:43:19

Ethin, It's mean, if I want to use the Socket library, and I need several clients, I not need multithreading?

2018-12-26 22:16:07

So, I read on the Internet.
There are two libraries. socket and twisted.
socket is a library in which I have to use multithreading, as I understood. Twisted higher level library.
I looked at the example codes, and I'm terrified. In the Protocol class, I write a server program, as I understand it. But Factory...
Where can I read about it, and how can I figure it out? After BGT it looks scary.

2018-12-27 00:10:58

you don't always need multi threading. You could write a server with select and socket and you would deal with multiple clients anyway.

Paul

2018-12-27 00:49:45

Hi,

uhm, what will probably confuse you in Python's socket library is the blocking mode. If you're just reading through the socket api quickly, you might end up with the idea that the operations are blocking and therefore you need to implement some multithreading to compensate for this. Well, its just as easy as telling the socket library to work non-blocking instead, so you can handle loads of clients with a simple thread, as long as your functions/methods are fast enough to prevent incoming requests from waiting too long. The Python multithreading is... well, it creates multiple python instances and therefore allows to process methods and functions in parallel, but its technically more multiprocessing than multithreading, since those threads won't actually be executed on the different cores of your processor, they will still run on one single thread. Writing true multithreaded programs in Python is rather complicated and shouldn't worry you too much if you're just a newbie migrating from BGT right now.
I'd recommend to play around a bit with sockets in Python, but if you're planning to program something consistant and stable, consider using a well-known library like Twisted. You'll find loads of example on the net, and yeah, its of course more complicated than BGT. BGT does many things really basic and simple, just to simplify audio game programming, but it doesn't do you any good when you're planning to learn real programming.
Best Regards.
Hijacker

2018-12-27 04:35:30

Hijacker, Can you advise how I can learn Twisted?

2018-12-27 05:04:43 (edited by magurp244 2018-12-27 05:07:03)

There's some guides [here], and [here] and some example scripts [here]. There's also the [documentation], with a slightly more up to date PDF [here].

I've played around with twisted a bit before, so I have a few examples around as well. Some of the more recent ones haven't been fully tested, mostly with timeouts and over open networks, but they handled more game oriented things like finding servers with UDP broadcasting across local networks, TCP/IP client/server setup with host migration, etc.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2018-12-27 09:31:06

Twisted is great, There's another library called podSixNet which is usually used for games network handlings.

---
Co-founder of Sonorous Arts.
Check out Sonorous Arts on github: https://github.com/sonorous-arts/
my Discord: kianoosh.shakeri2#2988

2018-12-27 11:24:08

magurp244, I read these links. It is very difficult. Synchronous and psychic programming ...
I should look for simpler information.

2018-12-28 08:07:13

It's harder than in C#. But I'd like to deal with it.
For example, why do I need Factory in Twisted? How it works?
If you know Twisted, how did you learn it?

2018-12-28 10:01:44

regarding podsixnet, if you are looking for tcp sockets in your game, it is really really good.
but, if you are looking for a lib that can handle udp, then podsixnet is not for you.

2018-12-28 10:49:53

Mostly by playing around with the examples, also reading through [1500 archers] for a better understanding of lock step networking and such. In simplist terms, the factory handles the connection process and calls a client class for handling connections when another computer reaches out, for example here's a basic TCP/IP server:

from twisted.internet import reactor, protocol

class Echo(protocol.Protocol):
    """This is just about the simplest possible protocol"""
    
    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print("Client Said: ",data)
        self.transport.write(data)

def main():
    """This runs the protocol on port 8000"""
    factory = protocol.ServerFactory()
    factory.protocol = Echo
    reactor.listenTCP(8000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

In this example, Main() is called which sets up the initial server. The server factory handles the process of establishing a connection, and assigns a connecting computer a protocol class for sending and recieving data, in this case, the Echo() class above. Then the reactor listens on port 8000 for any computer that tries to connect, if one does it passes it to the factory to handle the connection and assigns it the Echo class to communicate with it. Now, for the client example:

from twisted.internet import reactor, protocol

class EchoClient(protocol.Protocol):
    """Once connected, send a message, then print the result."""
    
    def connectionMade(self):
        self.transport.write(b"hello, world!")
    
    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print("Server said:", data)
        self.transport.loseConnection()
    
    def connectionLost(self, reason):
        print("connection lost")
        reactor.stop()

# this connects the protocol to a server running on port 8000
def main():
    f = protocol.ClientFactory()
    f.protocol = EchoClient
    reactor.connectTCP("localhost", 8000, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

This client calls main(), which establishes the Client Factory, and then tries to connect to a server on port 8000, if it succeeds it calls the client factory to handle setting up the connection with the server and assigns the EchoClient() class to handle sending and recieving data.

So, so sum it up: both the client and the server listen and reach out through ports, when a connection is detected they use factories to handle those incoming connections, and then assign them class handlers for sending and recieving data. When someone connects to a server, it creates an Echo class which you can stuff in a list somewhere, and then to use it you do something like:

self.clients[0].send("message for client one.")
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2018-12-28 11:59:08

kianoosh, visualstudio,
I'm focused on Twisted, but it is difficult to understand.
I looked at the implementation of sockets in C#, it is more simple than in Python.

magurp244,
Thank you so much for this! Already many times you helped me.
I didn't understand the details about the factory. I understud that the factory calls the server and client event classes. But thanks, it helped me.