0

Hopefully I can make this somewhat clear. I have to create a server and client in python that sends HTTP GET request to each other. Now I created a basic server/client program and now my goal is to send the server a HTTP GET request(from an html file I have saved) and the server to display the content. Example Server would display something like Date: Content Length: content Type: As of now when I run the client and type GET / filename.html HTTP/1.1\n\r the server does not display. When I type that exact command in just a Linux shell it displays perfectly. How can I do this in a client and server. Hopefully this makes sense. Here is my client and server.

 #CLIENT
import socket
import sys

host = socket.gethostname()    
port = 12345                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print(s.recv(1024))
inpt = input("type")
b = bytes(inpt, 'utf-8') #THIS SENDS BUT SERVER DOESDNT RECIEVE
s.sendall(b)
print("Message Sent")

#SERVERimport socket
import sys
host = ''        # Symbolic name meaning all available interfaces
port = 12345     # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
while True:
  c, addr = s.accept()
  print("Connection accepted from " + repr(addr[1]))
  c.send(b"Server Approved")
  print(repr(addr[1]) + ":" + c.recv(1024).decode("UTF-8"))
  c.close()

I want to type something like this to my server and display its content
GET / google.com HTTP/1.1\r\n

johnny 5
  • 19,893
  • 50
  • 121
  • 195
VinAbb
  • 25
  • 2

1 Answers1

0

The error message is so straightforward, just read it to fix the code that doesn't work.

Traceback (most recent call last):
    ...
print(repr(addr[1]) + ":" + c.recv(1024))
TypeError: must be str, not bytes   

Add .decode("utf-8"):

print(repr(addr[1]) + ":" + c.recv(1024).decode("utf-8"))

And more clear is to use str() instead of repr(). You can read about the differences between these two here: Difference between __str__ and __repr__ in Python.

print(str(addr[1]) + ":" + c.recv(1024).decode("utf-8"))
radzak
  • 2,986
  • 1
  • 18
  • 27
  • Even with the .decode imputed I'm still getting TypeError: unsupported operand type(s) for +: 'int' and 'str' – VinAbb Apr 06 '18 at 01:10
  • My bad, addr[1] is an `int` so either `str()` or `repr()` is necessary. – radzak Apr 06 '18 at 01:17
  • Hmm both still don't seem to work. But thank you for the .decode portion it makes sense to change the byte into a string – VinAbb Apr 06 '18 at 01:23
  • Please update the question with the error or/and description of the problem you have right now, so I can help. – radzak Apr 06 '18 at 01:25
  • just updated code and displayed the HTTP GET method I want to type in and display – VinAbb Apr 06 '18 at 01:34
  • you mean that you want '\r\n' appended to each of the messages sent from the client? and displayed exactly like that on the server? – radzak Apr 06 '18 at 01:57