Search code examples
pythonhttpweb-applications

How to get a file or a response from the server I just made


I am trying to learn Django through the course offered by MichiganX on edX. My problem is that the lesson skipped some details, and without them I cannot get the code to work.

Code looks like this:

from socket import *
  def createServer():
  serversocket = socket(AF_INET, SOCK_STREAM)
  try:
      serversocket.bind(('localhost', 9000))
      serversocket.listen(5)
      while 1:
          (clientsocket, address) = serversocket.accept()

          rd = clientsocket.recv(5000).decode()
          pieces = rd.split('\n')
          if len(pieces) > 0:
              print(pieces[0])

          data = "HTTP/1.1 200 OK\r\n"
          data += "Content-Type: text/html; charset=UTF-8\r\n"
          data += "\r\n"
          data += "<html><body>Hello World</body></html>\r\n\r\n"
          clientsocket.sendall(data.encode())
          clientsocket.shutdown(SHUT_WR)
  except KeyboardInterrupt:
      print("\nShutting down...\n");
  except Exception as exc:
      print("Error:\n");
      print(exc)
  serversocket.close()


print('Access http://localhost:9000')
createServer()

We were also told to build a simple web browser that would connect us to the server we build and it should return "Hello World!", code for it looks like this:

 import socket

  mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  mysock.connect(('127.0.0.1', 9000))
  cmd = 'GET http://127.0.0.1/romeo.txt HTTP/1.0\r\n\r\n'.encode()
  mysock.send(cmd)

  while True:
      data = mysock.recv(512)
      if len(data) < 1:
          break
      print(data.decode(), end = '')

  mysock.close()

First problem is that nowhere in the course there's a mention of romeo.txt, it just popped out of nowhere. They told us to just run the code, but it does nothing. I googled how to launch server so in my command windom I launched it from the directory my server file is located but whenever I try to connect with above web browser (even if I delete "romeo.txt") from code I get error 404.

I know I'm missing something, I know that just launching server from the directory my files are located are not enough, lesson doesn'n explain how to do it but in order to progress in the next lesson I need to do it. Can anyone help me? Sorry if the question is stupid but I'm stuck for two hours already and I have no idea what else can I do.


Solution

  • These two programs are creating sockets.
    The first program acts as the server and the second program acts as the client. The first program is waiting for someone to connect to it as in this line serversocket.accept(), while the second program tries to connect to the server as in this line mysock.connect(('127.0.0.1', 9000)).
    Your problem is you are not running the server while running the client. The solution is first start the server, keep it running then start the client.
    It should work like so, in one cmd / terminal:
    python server.py
    In other cmd / terminal:
    python client.py
    Replace server.py and client.py with original file names.