Archive for the 'socket' Category


faster way to find a cookie

In my previous post,, I showed how you can use a cookie from the web browser to speed up the scraping process. However, due to the fact that a website can have more than a cookie, this process is a bit error prone ( because you have to concatenate all the values in a string ). While testing something with netcat, I found a faster way, that is virtually without errors.

All you have to do is to start netcat and set it to listen on a specific port. Here’s how you do that:

nc -l -p 9000

Next, you have to configure your browser’s proxy to “localhost” , port 9000 ( or whatever port you specified ). Here is how you do it in firefox . Go to Options/Preferences, and then get to this screen:
From here, click on Settings, and fill in the proxy related details. After you’ve done this, visit the site to which you want to find the cookie. Look in the terminal/console you opened nc in and you will see the HTTP request. Look for the Cookie header, and copy it. From here on, you can follow the steps in the other article.


simplest way to use select with sockets?

I’ve been playing recently with python and network programming. IMHO, writing a server that makes use of select seems to be the simplest select code I’ve ever read/wrote. Here’s how it goes:

import socket,select,sys,pdb

if(len(sys.argv) != 2):
	print "a port must be supplied"
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
read_list = [sock]

while 1:
	current_data = ""	
	infds,outfds,errfds =,[],read_list,0.05)
	if len(infds):
		for socket in infds:
			if socket==sock:
				clientsock,client_addr = sock.accept()
				current_data = socket.recv(4196)
				if(not len(current_data)):
					print "client disconnected"
		for socket in [socket for socket in read_list if socket != sock]:
			if current_data != "":
				socket.send("a client wrote:%s"%current_data)

The code illustrates a VERY basic messaging “application”, that will listen on a user-supplied port. Each time a client writes a message, the server sends the message to all connected sockets. The message doesn’t get sent to server again ( notice the socket != sock ). The deal with select is that you need to supply it with a list to check for available data, this is the read_list. I’m no socket guru, but this really looks simple, right?

Blog Stats

  • 264,621 hits