Sometime the life has a distorted sens of humor and make a hole in your sock wen you are going to work.
It make feel you strange because you fell it and you only want to pull off both of them but Irony come in your live looking like a mail:
we need to connect to [last thing buy for the boss] system for [stuff i don’t understand/like/want] reason so your team need to make a sock to talk with this system.
[human(?) in my personal blacklist]
So I have a broken sock and I neet to make a socket.
So how to make a socket?
In python all you need is in the core of the language. If you want to start you only need to import socket and start coding.
After this we add some parameter like the host, the port and the size of the socket.
Now we need a socket object. Usualy i make it into custom object or function like this
- Start a socket with socket.socket()
- Set the socket to the Host and Port with .bind((HOST, PORT))
- Set the max connection with .listen(20)1
After this you need to accept new incomming request. I also set a timeout because I don’t want a connection unused on my socket.
We put into data_raw the data from the connection (here we use size)2 and we have 2 more interesting object:
- conn a socket with this connection
- address the ip for this connection
Now we have the data and we need to do something with this
And after you do what you need to do close the connection.
This is a complete example of a working socket. If you need to response you can use conn.send() to send the data to the client.
If you need help or not understand something write me in the comment below or to my social.
This is the full code for Python33
If you liked this article,
please share it on