Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Python Socket Programming Basic Part 1: Accepting - No voice tutorial

Custom Title Activated
Loyal Member
Joined
Oct 26, 2012
Messages
2,357
Reaction score
1,086
Hello guys, today I give you a tutorial about how to code sockets in Python (can also be done in IronPython)

First of all, I open my starting .py file and I remove every line.

The code of my .py:

PHP:
from socket import *

addr = ('127.0.0.1', 30000)

sock = socket(AF_INET, SOCK_STREAM, 0)
sock.bind(addr)
sock.listen(10)

print 'Bound sockets on ' + addr[0] + ':'  + addr[1].__str__()

while (True):
    new = sock.accept()

    acc_addr = new[1]
    ip = acc_addr[0]
    connid = acc_addr[1]

    print 'Received connection from ' + ip + ':' + connid.__str__();

Let's explain how the code works.

PHP:
from socket import *

This is the (most) important code. This includes all the things you need from the socket type.

Without that line:

PHP:
socket: <no type information available>

With that line:

PHP:
socket: type socket

Ok, moving on.

PHP:
addr = ('127.0.0.1', 30000)

This is for setting the IP:pORT network EndPoint. 127.0.0.1 is the IP (0.0.0.0 = Any IP) and 30000 is the port (I guess 0 is any port but can be wrong)

PHP:
sock = socket(AF_INET, SOCK_STREAM, 0)

This sets the socket. AF_INET is most used IPv4 network. SOCK_STREAM is streaming socket. 0 is 99% good.

PHP:
sock.bind(addr)

This binds the EndPoint to the socket. This sets the IP and port of the socket to the tuple addr.

PHP:
sock.listen(10)

This listens the socket, making it able to accept connections. The parameter is the backlog queue.

PHP:
print 'Bound sockets on ' + addr[0] + ':'  + addr[1].__str__()

This prints out the sockets has been started.

PHP:
while (True):

This loop will run forever, until you close the program.

PHP:
    new = sock.accept()

Set a new variable. It's a tuple of the connection. The first value (new[0]) is socket information, the second value (new[1]) is tuple of IP:connection Id(?).

PHP:
    acc_addr = new[1]

acc_addr is the tuple with the IP and the connection id(?). acc_addr[0] = IP and acc_addr[1] = the connection id(?)

PHP:
print 'Received connection from ' + ip + ':' + connid.__str__();

Prints out the data of accepted connection.

Next part comes receiving. Thanks for reading hopefully I didn't made any mistakes else correct me. Gimme a like, reputation up and a cookie if you like this.

Maybe I might do a video tutorial (and more video tutorials) but my voice is really bad. Sorry for bad English sometimes.
 
Joined
Dec 14, 2011
Messages
515
Reaction score
126
Good tutorial but i think if you wanne create a big program like a server or a game , it#s better to code it in c++

C++ is the other option but if you prefer your own language why not stick to it? and ya Python could be use for Large Projects along with the modules or numpy or pypy for example for a great performance, and what you can do or create with other languages python also does just like others ;)


Sent from my iPhone
 
Joined
Dec 14, 2011
Messages
515
Reaction score
126
I'm using C++ currently, however, I'm not so great in it :p
The reason why I made this is because I wanted to learn another programming language

nice bro and even senior c++ programmers says its really hard XD and ya Python would be the best choice despite the slow performance (you can use numpy etc. to make it fast) im using it for over 5 years now of full python.


Sent from my iPhone
 
Custom Title Activated
Loyal Member
Joined
Oct 26, 2012
Messages
2,357
Reaction score
1,086
nice bro and even senior c++ programmers says its really hard XD and ya Python would be the best choice despite the slow performance (you can use numpy etc. to make it fast) im using it for over 5 years now of full python.


Sent from my iPhone
And now I use it even more because I learn it at school.
Python is pretty easy, but it isn't that great.
 
Back
Top