im.py: a python communications tool

Jake D jhunter.dunefsky at gmail.com
Fri Apr 5 20:16:05 EDT 2013


Hey Usenetites!
I have a horrible Python program to allow two people to chat with each
other.  It has horribly any functionality, but it is meant for the
public to work on, not necessarily me.  Anyways, here's a quick FAQ.

What does this do that IRC can't?  What does this do that AIM can't?
--It allows direct communication between two computers, whereas IRC
doesn't.  And AIM and similar services require a username, etc.  This
is made specifically for two users on a network to chat.
What version of Python is this written in?
--Python 2.7.3.
What is the licence?
--It's released under a special FOSS licence.  Here it is:
----You can do whatever you want with this program.

Alright, now, here's the code:

#!/usr/bin/python
#An instant messaging program implemented in Python.
#Created on Sunday, December 30, 2012 (long before it's Usenet
publication)

import socket
import sys
import threading

def server_listen():
	while True:
		r = c.recv(8192)

		if r == "\quit":
			c.close()
			s.close()
			sys.exit(0)

		print con_addr[0], ": " + r

def client_listen():
	while True:
		r = s.recv(8192)

		if r == "\quit":
			s.close()
			sys.exit(0)

		print sys.argv[1], ": " + r

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

if sys.argv[1] == "-l":
	s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	s.bind(('', 5067))
	s.listen(5)
	c, con_addr = s.accept()

	while True:
		r = c.recv(8192)

		if r == "\quit":
			c.close()
			s.close()
			sys.exit(0)

		print con_addr[0], ": " + r
		i = raw_input("You: ")

		if i == "\quit":
			c.send("\quit")
			c.close()
			s.close()
			sys.exit(0)

		c.send(i)

else:
	s.connect((socket.gethostbyname(sys.argv[1]), 5067))
	print "Chat initiated with " + sys.argv[1] + "!"

	while True:
		i = raw_input("You: ")

		if i == "\quit":
			s.send("\quit")
			s.close()
			sys.exit(0)

		s.send(i)
		r = s.recv(8192)

		if r == "\quit":
			s.close()
			sys.exit(0)

		print sys.argv[1] + ": " + r

I encourage people to modify this code, because really, it sucks.
Enjoy!



More information about the Python-list mailing list