Category Archives: Python

Building your own game using pygame

These days I was experimenting with pygame and found out that how easy game development can be done with pygame. I build a ‘Space War’ clone using pygame with some sound effects. Now I’m making a HOWTO on building the games using pygame. When it is complete I will post it here..

Space War

[DOWNLOAD SPACEWAR SOURCE]

Advertisements

1 Comment

Filed under Linux, Programming, Python

Python Networking: Send Emails using smtplib

Here is a small application in Python for sending email messages via SMTP

=====================================================

#!/usr/bin/python
import smtplib

fromAddr = raw_input(‘From: ‘)
toAddr = raw_input(‘To: ‘)

Subject = raw_input(‘Subject: ‘)
Message = raw_input(‘Message: ‘)

Email = ‘Subject: ‘+Subject+’\n\n’+Message

Server = smtplib.SMTP(“BlackPearl”,25)
Server.sendmail(fromAddr, toAddr, Email)
print ‘Your Email has been sent to ‘, toAddr

====================================================

If your system has two users “root” and “leaf” you should have two email accounts like root@localhost and leaf@localhost (Assuming hostname to be localhost. Here in this example its BlackPearl)

Run using python email_smtp.py

To view the email type mail and type the email number to read it.

Leave a comment

Filed under Linux, Programming, Python

Python Socket: Updated Client Server

This is a modification of the previous one

#!/usr/bin/python
import socket
import sys

if len(sys.argv) < 3:
<tab>print ‘Usage: %s [hostname] [portnumber]’ % sys.argv[0]
<tab> sys.exit(1)

server = sys.argv[1]
port = int(sys.argv[2])
msg = ‘start’

#Setup a standard internet socket. Connects to a server
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((server,port))
while msg != ‘exit’:
<tab> msg = raw_input(‘Enter a Message: ‘)
<tab>sock.send(msg)
<tab>data = sock.recv(1024)
<tab>print ‘Received Message: ‘, data
sock.close()

#!/usr/bin/python
import socket
import sys

if len(sys.argv) < 3:
<tab> print ‘Usage: %s [hostname] [portnumber]’ % sys.argv[0]
<tab>sys.exit(1)

hostname = sys.argv[1]
port = int(sys.argv[2])
msg = ‘start’

#Setup a standard internet socket.
#The sockopt call lets this server use the given port even if
#it was recently used by another server
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind((hostname,port))
sock.listen(1)
print ‘Waiting for a Request’

#Handle a client request
request,clientAddress = sock.accept()
print ‘Request received from: ‘, clientAddress
while msg != ‘exit’:
<tab> data = request.recv(1024)
<tab>print ‘Received Msg: ‘, data
<tab> msg = raw_input(‘Enter a Message: ‘)
<tab>request.send(msg)
request.shutdown(2) #Stop the client from reading or writing anything.
sock.close()

Download Source

Leave a comment

Filed under Linux, Programming, Python

Socket Programming in Python

This post is for my classmates who finds it difficult to program in Java (To hell with Java’s unfair ). Python helps you here.

Here is a sample Client-Server program that sends messages bidirectionally.

Server.py

#!/usr/bin/python
import socket
import sys

if len(sys.argv) < 3:
<tab>print ‘Usage: %s [hostname] [portnumber]’ % sys.argv[0]
<tab>sys.exit(1)

hostname = sys.argv[1]
port = int(sys.argv[2])

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind((hostname,port))
sock.listen(1)
print ‘Waiting for a Request’

request,clientAddress = sock.accept()
print ‘Request received from: ‘, clientAddress
data = request.recv(1024)
print ‘Received Msg: ‘, data
request.send(‘Server Message’)
request.shutdown(2)
sock.close()

Client.py

#!/usr/bin/python
import socket
import sys

if len(sys.argv) < 3:
<tab>print ‘Usage: %s [hostname] [portnumber]’ % sys.argv[0]
<tab>sys.exit(1)

server = sys.argv[1]
port = int(sys.argv[2])

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((server,port))
sock.send(‘This is the Message from the Client’)
data = sock.recv(1024)
print ‘Received Message: ‘, data
sock.close()

Execution Tips

python Server.py <hostname> <portnumber> (in a terminal)

python Client.py <hostname> <portnumber> (in another terminal)

Download Source

Leave a comment

Filed under Linux, Programming, Python

Google Summer of Code 2008

Hi its been a long since I ve been on my blog.

Im now planning to participate in the Google Summer of Code 2008. I would like to work on wxPython Teaching Tool project in the wxPython section. Im not sure whether I will be assinged to that project, but im really interested in it because Python is my favourite programming language and I’ve already done a GUI project in python at my college using Tkinter & Pmw (Digital Circuit Simulator) . wxPython is not very different from Tkinter rather it has more GUI widgets.

Also I would like to learn more about wxPython and I think this is my best opportunity to do it. So I’ll try my best to get that project

Leave a comment

Filed under GSoC, Linux, Personal, Programming, Python

Digital Circuit Simulation in Python

Here is my Mini- Project ie, Digital circuit simulator. Check it out. If you find any modifications, please tell me
Here is the Link

Download Source

Leave a comment

Filed under Linux, Personal, Programming, Python

Mini Project Started

Our Mini-Project for the sixth semester is Digital Circuit Simulation in Python. This project uses the Tkinter module for creating the graphical interface of the simulator.

So far, the base system containing simulations of basic gates like AND, OR, NOT, NAND, NOR , XOR etc and other digital circuits like Adders, Subtractors, Counters, Lathces, Flip-flops etc are designed. Next, we have to apply a graphical interface to the system using Tkinter widget set to the base system.

I think that this Project will be helpful for the teachers and students who are teaching and studying digital circuits. This will be also helpful to the circuit designers to simulate their circuits using a computer before they are implemented in circuits

Leave a comment

Filed under Linux, Personal, Programming, Python