5 Replies - 2385 Views - Last Post: 09 March 2009 - 03:36 AM Rate Topic: -----

#1 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 151
  • Joined: 15-December 08

Just some help with one-way messaging service

Posted 03 March 2009 - 11:55 AM

I wrote this simple app about a year ago, and just today I decided to look for it again and see where have I got with the code.

A friend of mine did some editing with the program, and now I can't figure out how to use this program since he changed some components of it and neglected to inform me how it works.

The app is a one-way messaging service where the user can send messages to another user.

#!/usr/bin/python

import socket as sock
import threading, sys, time

print ("subliminal messages")
print ("written by hdr")
print ("tweaked by Richo")

if len(sys.argv) != 2 or sys.argv[1] not in ('--client', '--server'):
	sys.stderr.write("Usage %s --[client|server]")
	sys.exit(1)

type = {"--client": "client", "--server": "server"}[sys.argv[1]]
IP = raw_input('IP: ')
PORT = raw_input('PORT: ')
 
class submclient(threading.Thread):
		def __init__(self, host, port):
				threading.Thread.__init__(self)
				self.host = host
				self.port = port
				self.sock = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
				self.ready = False
				self.sendqueue = []
		def send(self, msg):
				if self.ready:
						self.sock.send(msg+"\\n")
				else: self.sendqueue.append(msg)
		def run(self):
				try:
						self.sock.connect((self.host, self.port))
						print ("%s:%d OPEN") % (self.host, self.port)
					#self.sd.close()
						self.ready = True
				except:
						self.sock = None
						sys.exit(1)
				if self.sendqueue != []:
						for i in self.sendqueue:
								self.sock.send(i+"\\n")
								del self.sendqueue
						while self.sock != None:
								data = self.sock.recv(1024)
								if not data:
										##kerplode
										sys.exit(1)
										sys.stdout.write(data+"\\nMessage: ")
class submserver(threading.Thread):
		def _init_(self, port):
				threading.Thread.__init__(self)
				self.port = port
				self.ready = False
		def send(self, msg):
				if self.ready:
						self.sock.send(msg+"\\n")
				else:
						self.messagequeue.append(msg)
		def run(self):
				self.host = ''
				self.addr = (self.host, self.port)
				self.buf = 1024
				sock.listen(1)
				self.ready = True
				if self.messagequeue != []:
						for i in self.messagequeue: self.sock.send(i+"\\n")
				while self.sock != None:
						data = self.sock.recv(1024)
						if not data:
								## kerplode
								sys.exit(1)
						sys.stdout.write(data+"\\nMessage: ")
if type == "client":
		mySock = submclient(IP, PORT)
		mySock.start()
if type == "server":
		mySock = submserver(PORT)
		mySock.start()
time.sleep(2) ## REALLY UGLY HACK
			  ## let the connection sort itself out, too lazy to code up a nice lock
while mySock.sock != None:
		mySock.send(raw_input("Message: "))



It's just I'm confused on how the app works now and whether it is complete or not for that matter, so anyone who can shed some light on this is highly appreciated.

I haven't used python for more than a year, so I've forgotten quite a bit about it unfortunately.

Most of the comments are written by my friend, btw.

Is This A Good Question/Topic? 0
  • +

Replies To: Just some help with one-way messaging service

#2 mrgoo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-March 09

Re: Just some help with one-way messaging service

Posted 03 March 2009 - 12:52 PM

Just having a quick look at this. I could tell you more if I spent a little longer looking, but as I am in the middle of coding my latest project I can tell you that you have established a simple server client application that allows you to take raw input from the user and transmit it to the other end by specifying the port. Through a series of checks the application tests if the other side is ready to receive. How's that? need any more info?
Was This Post Helpful? 0
  • +
  • -

#3 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 151
  • Joined: 15-December 08

Re: Just some help with one-way messaging service

Posted 06 March 2009 - 12:08 AM

Well, if I try to run it in IDLE it comes out like this:

subliminal messages
written by fuser
tweaked by Richo
Usage %s --[client|server]Traceback (most recent call last):
File "D:\Python30\subm", line 12, in <module>
sys.exit(1)
SystemExit: 1

is there anyway I can fix this problem?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Just some help with one-way messaging service

Posted 06 March 2009 - 08:40 AM

Well this:
if len(sys.argv) != 2 or sys.argv[1] not in ('--client', '--server'):
    sys.stderr.write("Usage %s --[client|server]")
    sys.exit(1)


Should be:
if len(sys.argv) != 2 or sys.argv[1] not in ('--client', '--server'):
    sys.stderr.write("Usage %s --[client|server]\n" % sys.argv[0])
    sys.exit(1)



But in any event, the reason it's doing that in IDLE is because it expects either --client or --server on the command line; i.e.,
./sublim.py --client

and you're not passing the argument it expects.
Was This Post Helpful? 0
  • +
  • -

#5 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 151
  • Joined: 15-December 08

Re: Just some help with one-way messaging service

Posted 09 March 2009 - 03:35 AM

ok, sorry for the late update. Following Jack's advice, I tried running it in the console instead, (I use Windows though) so there is an error when i tried running the server part of the prgoram:

Traceback (most recent call last):
File "subm", line 78, in <module>
mySock = submserver(PORT)
File "D:\Python30\lib\threading.py", line 410, in __init_
assert group is None, "group argument must be None for
Assertionerror: group argument must be None for now


I can't see anything wrong with it, can anyone see what is the issue here?
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Re: Just some help with one-way messaging service

Posted 09 March 2009 - 03:36 AM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1