Python sockets

griimnak

You're a slave to the money then you die
Jul 20, 2013
955
794
What is a socket?
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.

If you're someone that mods habbo, you use sockets quite frequently when running an emulator with a dedicated game port and MUS port. For example, a habbo client will connect to your emulator which is streaming on socket 30000.

Creating and interacting with sockets via Python
Right off the bat, you'll want to import python's built in socket module.
PHP:
import socket

This is an example socket server to listen for incoming data:
PHP:
import socket
import threading

class SocketServer(object):
    def start(self):
       # Variables
        bind_ip   = '0.0.0.0'
        bind_port = '999999'
        # Create server instance
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.bind((bind_ip,bind_port)) # bind
        server.listen(5) # listen
        print "[*] Listening on %s:%d" % (bind_ip,bind_port)

        def handleConnection(client_socket):
            request = client_socket.recv(1024)
            if request == 'poop':
                 print 'HAX! ULTIMATE HAX!'

            print "[+] Received: %s" % request
       # if there's a request to connect, do this;
        while True:
            client,addr =  server.accept()
            print "[*] Accepting connection: %s:%d" % (addr[0],addr[1])

            client_handler = threading.Thread(target=handleConnection,args=(client,))
            client_handler.start()


Now you can use this client to send data to the listening server:
PHP:
import socket

class Messager(object):
    def requestInput(self):
        target_ip   = ''
        target_port = ''


        print '[*] Client now bound to %s:%d' % (target_ip, target_port)

        while True:
            inputfield = raw_input("[*] >> ")

            if inputfield == '/ping':
              
                print 'pingping!'

            elif inputfield != '':
               # client instance
                client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                client.connect((target_ip, target_port))
                client.send(inputfield)

Explanation
On the server, you should configure bind_ip and bind_port to the desired port and ip you wish to bind the server to. If you wish to run the server local, the ip should either be 127.0.0.1 or 0.0.0.0.
On the client, you should configure target_ip and target_port to match the details of your server. With that done, your server will listen on your ip:port. You can then you use your client to send text data via TCP sockets, with python!

With alot of logic, you can do anything with sockets. From game development to remote administration tools, anything is possible really.
I included some dummy logic in the server. If it receives "poop" it will spit some shit out.

Your output should look something like this (minus the pretty stuff):
KdUYDOJ.gif


Python Sockets Documentation
Official python docs: &
IBM developers docs:

AND you can download the source to the aegis test server above if you want;
cheers
 
Last edited:

Users who are viewing this thread

Top