Socket MUD client script

Z

Zamnedix

I'm having trouble with this script I'm writing. I want it to connect
to a MUD, which it does fine, but afterwards it displays the greeting
and login prompt, and when I type in username the loop to receive and
then send seems to stop.


Here's my code:

#!/usr/bin/python
import socket
import sys
a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = str(raw_input("HOST:pORT > "))
address = address.split(":")
a.connect((address[0], int(address[1])))
print "CONNECTED"
while 1:
print a.recv(4079)
print a.recv(4079)
msg = str(raw_input("MSG > "))
a.sendall(msg)

Here's what happens:
$ python connect.py
HOST:pORT > lofe.org:8000
CONNECTED


....._
((big ascii art greeting, staff info and stuff))

Enter your character's name, or type new:
MSG > Xeythos

la la la la la la

^CTraceback (most recent call last):
File "connect.py", line 18, in ?
print a.recv(4079)
KeyboardInterrupt
$



Also, I have to receive twice to get the whole greeting, and it comes
really slowly in two pieces.
I've looked around the internet for a good tutorial on python and
sockets but haven't really found anything. Am I using the wrong type
of socket, or what? Someone please help.
 
M

MRAB

Zamnedix said:
I'm having trouble with this script I'm writing. I want it to connect
to a MUD, which it does fine, but afterwards it displays the greeting
and login prompt, and when I type in username the loop to receive and
then send seems to stop.


Here's my code:

#!/usr/bin/python
import socket
import sys
a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = str(raw_input("HOST:pORT > "))

'raw_input' returns a string, so there's no need to use 'str'.
address = address.split(":")
a.connect((address[0], int(address[1])))
print "CONNECTED"
while 1:
print a.recv(4079)
print a.recv(4079)
msg = str(raw_input("MSG > "))
a.sendall(msg)
[snip]

It might be expecting the string to end with '\n' or '\r\n'.
 
T

Terry Reedy

Zamnedix said:
I'm having trouble with this script I'm writing. I want it to connect
to a MUD, which it does fine, but afterwards it displays the greeting
and login prompt, and when I type in username the loop to receive and
then send seems to stop.


Here's my code:

#!/usr/bin/python
import socket
import sys
a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = str(raw_input("HOST:pORT > "))
address = address.split(":")
a.connect((address[0], int(address[1])))
print "CONNECTED"
while 1:
print a.recv(4079)
print a.recv(4079)
msg = str(raw_input("MSG > "))
a.sendall(msg)

Here's what happens:
$ python connect.py
HOST:pORT > lofe.org:8000
CONNECTED


....._
((big ascii art greeting, staff info and stuff))

Enter your character's name, or type new:
MSG > Xeythos

la la la la la la

^CTraceback (most recent call last):
File "connect.py", line 18, in ?
print a.recv(4079)
KeyboardInterrupt
$



Also, I have to receive twice to get the whole greeting, and it comes
really slowly in two pieces.
I've looked around the internet for a good tutorial on python and
sockets but haven't really found anything. Am I using the wrong type
of socket, or what? Someone please help.

I suspect you are humg in the second a.recv since the response to the
username is completely swallowed by the first one. That said, a mud
client is basically a telnet client. So I recommend you use the
telnetlib module. Telnet objects have a .read_until method, so your
initial code wold mostly be replaced with

tn = telnetlib.Telnet(address[0], int(address[1], 30)
tn.real_until(b"or type new: ") #remove b prefix for 2.x

I recommend entering the loop until logged in. After that, you should
get a consistent prompt to read up to.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top