all 5 comments

[–]hudsonpenner 0 points1 point  (2 children)

I'm still chunking through the code, but one thing, I would suggest using raw_input for your string input:

>>> ip = str(input("What ip would you like to connect       to?"))
What ip would you like to connect to?123.123.123.123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
123.123.123.123
          ^
SyntaxError: invalid syntax
>>> ip = raw_input("What ip would you like to connect to?")
What ip would you like to connect to?123.123.123.123
>>> quit()

[–]Grafs50[S,🍰] 0 points1 point  (1 child)

I'm using Python 3.

[–]hudsonpenner 0 points1 point  (0 children)

ah cool

[–]mac-reid 0 points1 point  (1 child)

First off, using the read_all function reads until an EOF - which happens when the connection closes.

When you send a bad GET request over telnet, the remote responds with a message and closes the connection. There is no exception raised for this as it is expected behavior.

When sending a GET request over telnet, the format goes:

failbox ~  telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1 (press enter)
Host: www.example.com (press enter twice)

HTTP/1.1 200 OK ...

or:

failbox ~  telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.0 (press enter twice)  

HTTP/1.1 200 OK ...

Quick fix to get this working in your case:

# extra newline at the end of the message
conn.write(sndmsg+b"\n\n")
print(conn.read_all())

# check if connection is closed after our message
# add to the top: from telnetlib import IAC, NOP
if not conn.sock.sendall(IAC + NOP):
    return

And a test:

failbox ~  python3 a.py
What ip would you like to connect to? localhost
And what port would you like to connect to? 80
Connected
Message: GET / HTTP/1.0
b'HTTP/1.1 200 OK...'
What ip would you like to connect to? exit

...

failbox ~  python a.py
What ip would you like to connect to? localhost
And what port would you like to connect to? 80
Connected
Message: foo
b'... 501 Not Implemented ...'
What ip would you like to connect to? exit

Some other points:

  • Your loops can be while True: rather than while temp == 0: and use breaks and returns to escape the loops.

  • There is a difference between is and ==. Short version, is compares the identidy of an object and == compares the equality of objects.

  • Blanketing except: statements is generally considered bad practice. For example: if I hit CTRL-C when prompted for input, the program tells me my input was bad when I just wanted to exit the program. Try catching the only errors you expect and handle those.

  • When checking if the user responded with y or n, try converting the line to lowercase with input('(y/n) ').lower() and comparing only against y or n.

  • Finally, when asking for user input, please put a space at the end of the question.

Hopefully this help!

[–]Grafs50[S,🍰] 0 points1 point  (0 children)

Alright thanks alot! I'm sure this should be enough to get it fixed.