- 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:
Let's explain how the code works.
This is the (most) important code. This includes all the things you need from the socket type.
Without that line:
With that line:
Ok, moving on.
This is for setting the IPORT 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)
This sets the socket. AF_INET is most used IPv4 network. SOCK_STREAM is streaming socket. 0 is 99% good.
This binds the EndPoint to the socket. This sets the IP and port of the socket to the tuple addr.
This listens the socket, making it able to accept connections. The parameter is the backlog queue.
This prints out the sockets has been started.
This loop will run forever, until you close the program.
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(?).
acc_addr is the tuple with the IP and the connection id(?). acc_addr[0] = IP and acc_addr[1] = the connection id(?)
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.
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 IPORT 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.