Creating sockets with Python

| No Comments
Python programming language has a socket object, which you can use to build a network socket. To build a client that connects to a remote host and sends some arbitrary text, utilize the following code.

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 4242))
s.send('Some Arbitrary Text')
data = s.recv(1024)
s.close()
print 'Received', 'data'

Now you can connect to this with a netcat listener:

nc -l -p 4242

You should receive "Some Arbitrary Text" as the output. Can you start to see the practical applications? :-)

Leave a comment

About this Entry

This page contains a single entry by Jeff published on February 10, 2010 9:47 PM.

Simple Tips For Better Password Security was the previous entry in this blog.

Ipod, or Iphone? is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.