Search code examples
pythonservertelnet

building a server, telnet localhost 8888 showing blank screen


so I'm trying to get through this tutorial here .

I started by running the code in a file here:

import socket

HOST, PORT = '', 8888

listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print('Serving HTTP on port %s ...' % PORT)
while True:
    client_connection, client_address = listen_socket.accept()
    request = client_connection.recv(1024)
    print(request.decode('utf-8'))

    http_response = """\
HTTP/1.1 200 OK

Hello, World!
"""
    client_connection.sendall(bytes(http_response, 'utf-8'))
    client_connection.close()

then in the next part, it tells me to enter this line on the same computer.

$ telnet localhost 8888

I have webserver1.py running on a different cmd window (in windows). when I run this line though I get a blank screen instead of

$ telnet localhost 8888
Trying 127.0.0.1 …
Connected to localhost.

like I'm supposed to. Does anyone know why? How to fix it? I tried googling telnet localhost 8888 to no avail, I couldn't find a situation where this happened before.


Solution

  • so uh... unless there's an http daemon running/listening on that port, which couldn't happen anyway because of how ports typically work (but not always), that's actually about as far as that exercise will go.

    The result you had was correct, based on the output you got after running telnet from another cmd session while that python script was running in the background. However, the windows oobe version of telnet is weaksauce. Using it to send additional commands to communicate with the server once a successful connection is established is kinda booty.

    The script invokes the python interpreter (shoulda used #!/usr/bin/env python... just saying) and uses a few objects from the socket lib to open a socket on port 8888 on the localhost (the computer you're currently logged into).

    That's it, nothing more. I ran through the exercise too, and gisted it @ https://gist.github.com/mackmoe/09de098b3df5c45adf7a17b764a1eec4

    Just my 2 cents here: If you want to setup a server for the experience (and for free), just grab virtualbox, a linux server iso and use one of digital ocean's walkthroughs. Like the one @ https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

    Hope That Helps Friend!