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]

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