Skip to content

Python 101: The Multithreaded Socket Server

Python is a great language to pick up for coders with just very basic experience with other languages, think HTML,CSS, etc. Today I’m going to be going over how to write your own Python socket server and for those of you that don’t know sockets are the way that computers make connections over a network. Anyway on to the guide now.

Socket’s 101

The basics of sockets would be to import the socket library to python and set up a simple socket.

from socket import * #This line imports socket.
serversock = socket(AF_INET, SOCK_STREAM) #Sets up a new socket known as serversock

Now if you want to bind the socket and use it as a server you need to add a few things to what we wrote above!

from socket import * #This line imports socket.
HOST = 'localhost' #Your computers name/IP
PORT = 50000 #The port your servers on.
BUFSIZ = 1024 #The size of the server's buffer.
ADDR = (HOST, PORT) #Sets a variable with both HOST and PORT.
serversock = socket(AF_INET, SOCK_STREAM) #Sets up a socket known as serversock
serversock.bind #Binds your socket to your HOST and PORT.
serversock.listen(5) #Listens for 5 simultaneous connections.

Allright now that you understand the basics of how these sockets work etc I’m just going to dump the full server code below if you have any more questions feel free to leave a comment or email me at ianmarmour@gmail.com.

Full Source Link

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: