Sock and socket

Fundor333 | | Reading time 3 minutes | Word count 494

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:

Hello

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.

Love

[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.

1
2
3
4
5
import socket

HOST = "0.0.0.0"
PORT = 9000
SIZE =8192

Now we need a socket object. Usualy i make it into custom object or function like this

6
7
8
9
def server_program():
	server_socket = socket.socket()
	server_socket.bind((HOST, PORT))
	server_socket.listen(20)

Where we

  • 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.

10
11
12
13
	while True:
		conn, address = server_socket.accept()
		conn.settimeout(60)
		data_raw = conn.recv(SIZE)

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

14
15
16
17
		# Sometimes you need this
		# data = data_raw.decode()
		do__somethings(adress,data_raw)
		conn.close()

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import socket

HOST = "0.0.0.0"
PORT = 9000
SIZE =8192

def server_program():
	server_socket = socket.socket()
	server_socket.bind((HOST, PORT))
	server_socket.listen(20)

	while True:
		conn, address = server_socket.accept()
		conn.settimeout(60)
		data_raw = conn.recv(SIZE)
		# Sometimes you need this
		# data = data_raw.decode()
		do__somethings(adress,data_raw)
		conn.close()

if __name__ == "__main__":
    server_program()

  1. In this case we use localhost for the host, 9000 for the port and 20 for the max connection but you can change it for your need. ↩︎

  2. Client and server must have the same size or bad this will be appening in your socket ↩︎

  3. Tested from 3.5 to 3.8 ↩︎


Comments

To reply to this post, you can send a Webmention or you can toot me at [email protected]
You mentioned this post on your site? Send a Webmention


See Also