Thursday, February 23, 2012

Wolfram Alpha

View this post here.

PS: I am taking my blog to wordpress. Sorry for inconvenience.

Pinterest

View this post here.

PS: I am taking my blog to wordpress. Sorry for inconvenience.

Saturday, February 11, 2012

Socket Programming: Python

We will be understanding the basics of socket programming using Python.



You may be thinking why socket programming? Well, its because if you need to send data over a network you need to know about socket. The HTTP websites runs on port 80. Each website has a IP adress. So when you request for a website actually your browser is trying to get data from someipaddress:80. 80 is default port that browser uses, otherwise specified.
Some of you might have used Apache. What it does it creates server(we call apache server) and binds it to 127.0.0.1 and port 80. 127.0.0.1 is called  loopback address as it tells the browser to look in the consumer's computer only instead of searching web.

Lets start, first of all socket is a connection point like your phone. You connect to some socket (someone on other side of phone) and send and receive message or data (chat).

Requires: Python 2.6 +

Using:
Lets try to create a server first.

  1. #! /usr/bin/python

  2. # Import all from module socket
  3. from socket import *

  4. # Defining server address and port 
  5. host = ''  #'localhost' or '127.0.0.1' or '' are all same
  6. port = 52000 #Use port > 1024, below it all are reserved

  7. #Creating socket object
  8. sock = socket()
  9. #Binding socket to a address. bind() takes tuple of host and port.
  10. sock.bind((host, port))
  11. #Listening at the address
  12. sock.listen(5) #5 denotes the number of clients can queue

  13. #Accepting incoming connections 
  14. conn, addr = sock.accept()

  15. #Sending message to connected client 
  16. conn.send('Hi! I am server') #send only takes string
  17. #Receiving from client
  18. data = conn.recv(1024) # 1024 stands for bytes of data to be received
  19. print data
  20.  
  21. #Closing connections
  22. conn.close()
  23. sock.close()
Now a client.
  1. #! usr/bin/python
  2. from socket import *
  3.  
  4. host = 'localhost' # '127.0.0.1' can also be used
  5. port = 52000
  6.  
  7. sock = socket()
  8. #Connecting to socket
  9. sock.connect((host, port)) #connect takes tuple of host and port
  10.  
  11. data = sock.recv(1024)
  12. print data
  13. sock.send('HI! I am client.')
  14.  
  15. sock.close()
Understanding:
Everything is explained in comments.

Note:
  • The program above is waiting for type of programming. Look at the send and recv part in both programming, if server is sending something client must always know when server will send and vice versa or it should wait for that. Of course if you don't want to drop anything you are sending.
  • # is used for comments except the first line it is for telling where python program is. Use path to python.exe on Windows.
  • Always use sock.close() to close the socket otherwise socket is in use error will be thrown by python. You may also see it if the program terminates in between.
  • 5 in sock.listen is of no use right now as the server.py will terminate as soon as it is done with first client.
  • sock.recv() waits till it does not receive something.
  • print data will not work with python 3.0+. Use print(data).
There will be more on it. Till if you want to read more about it, see here.

Monday, February 6, 2012

Blogger almost killed my blog!

Blogger has changed the domains of free blog from .com to .in. This may not be a big issue as .com redirects to .in domain.

I have withdrawn the post Creating a custom HTML tags. My reason for that is as I use syntax highlighter for the writing codes and it do not support .in domain. This thing had completely eliminated the css of my blog. Though you can see it my wordpress blog. Still you might not be able to see some codes written in other blog posts.

Worst part hits will get reduce drastically because search engines search for results first according to locale then common to world(like .com, .edu) and finally other country domains. Probably they wont even show the blogs  in results even if it finds the blog as perfect match. Like we don't get results from France or Mexico.