Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Python sockets
  • [Intro]
    Here's a basic tutorial for crafting sockets and such in Python. I'll be going over the basics, and include some basic sources. This is meant for newbies who want to get into networking on Python // Develop exploits etc.

    [Socket - What is it?]
    So before we continue, you need to know what a socket is. According to wikipedia:

    In computer networking, an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet.


    Basically, its a bi(two) directional connection across Internet protocols. So this is good for networking. In C, using sockets are much more difficult, but that's because C is a lower level language. But enough of C, lets get on with the Python.

    [Creating a socket]

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((\"www.iexploit.org\", 80))

    Thats a basic socket. Just about as basic as it gets. Lets go through this line by line.
    1: Importing the socket module
    2: Creating a new socket. We set 's' as the socket. Now lets look at the socket.SOCK_STREAM. This creates a new stream socket. We can also put SOCK_DGRAM for a Datagram socket connection. A socket stream creates a constant connection from client server. But a datagram socket doesn't create a constant connection(But its not pointless)
    3. This ones an easy one to figure out. This just connects to www.iexploit.org on port 80.

    So now we can create a simple server knowing this info.

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', 1259)) #Binding the server on port 1259
    s.listen(1) #Listens(obviously)
    while true: #Infinite loop to listen for connections
    details, chan = s.accept()
    print '\nConnection opened with: ', details)
    print chan.recv(200)
    chan.send('TEST')
    chan.close()

    Pretty simple. Should be pretty self explanatory. Now we can connect to it:

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 1259'))
    s.send('HI')
    s.recv(200)
    s.close()

    Simple. I shouldn't even have to explain it to you.

    Now thats just the basics of sockets. You can research more on the python.org docs section. Sockets are quite useful, but you can preform other socket actions with other libraries such as urllib. Maybe I'll make a tutorial on using urllib soon.

    --Chroniccommand
  • D0WNGRADE
    Posts: 220
    Nice post, networking is probably the thing I have the hardest time working with in python.
    This helped! :)
  • Sh3llc0d3
    Posts: 1,910
    I don't know Python but everyone should learn sockets in the language they know :)
  • Xin
    Posts: 3,251
    Thanks for this il definately use it to look back on as i use this for fuzzing and exploit development.
    Xin
  • undead
    Posts: 822
    Great tutorial. Very helpful.