0

I've been reading about Python - Network Programming and tried the code.

Looking at the print statement without parentheses, this code is meant for Python 2.

Since I'm using Python3, I've modified it.

Here is the updated code.

server.py

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print('Got connection from', addr)
   c.send('Thank you for connecting')
   c.close()                # Close the connection

client.py

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

s.connect((host, port))
print(s.recv(1024))
s.close()                     # Close the socket when done

Then I run both codes as instructed in the tutorial.

Following would start a server in background. $ python server.py &

Once server is started run client as follows: $ python client.py

This would produce following result −

Got connection from ('127.0.0.1', 48437) Thank you for connecting

However, the output that I'm getting is slightly different. Initially I ran python server.py. Nothing happened. Once I executed python client.py, I'm getting the following error.

user@linux:~$ python server.py 
Got connection from ('127.0.0.1', 59546)
Traceback (most recent call last):
  File "server.py", line 16, in <module>
    c.send('Thank you for connecting')
TypeError: a bytes-like object is required, not 'str'
user@linux:~$ 

user@linux:~$ python client.py 
b''
user@linux:~$ 

What's wrong with the codes and how to fix it?

  • 2
    you need to encode your message, so do `'Thank you for connecting'.encode()` and also, in `client.py` change to `recv(1024).decode()` – בנימין כהן Aug 18 '19 at 06:25
  • Thanks. It works! But why the message needs to be encode/decoded? –  Aug 18 '19 at 06:34
  • 1
    @d-coder, I didn't know message needs to be encoded/decoded when transmitting over a network. Btw, thanks found the answer here. https://www.quora.com/Why-is-encoding-needed?share=1 –  Aug 18 '19 at 06:47
  • The tutorial you are following is written for Python 2 (the opening page displays Python 2.4 [sic!]). You really want to find an introduction from this decade. It's not like it's hard to find good introductory materials for Python 3 any longer. Maybe see also http://nedbatchelder.com/text/unipain.html for an accessible explanation of the underpinnings for this particular difference. – tripleee Aug 18 '19 at 06:58
  • @Sabrina : Now you know why the messages needed to encoded when sending over the network. Pat yourself on the back! Good work.. :) – d-coder Aug 18 '19 at 07:04

2 Answers2

1

Instead of sending it like string you can try to encode the message in the next way:

...
msg = 'Thank you for connecting'
c.send(str.encode(msg))
...

and on the client side you can decode it back using

s.recv(1024).decode('utf-8')
Daniel Nudelman
  • 401
  • 5
  • 11
  • 1
    Server side code works, but not the client one. Can you share the full code on client side? –  Aug 18 '19 at 06:46
  • the basic problem with your code is that when you are opening a connection to the socket its expecting data as byte stream and getting string instead..so you could use something like b'your message' and then send it..it will solve the issue...i can help you with the code if you want. – mohor chatt Aug 18 '19 at 07:41
1

Simply like this

c.send(b'Thank you for connecting')

honglei
  • 71
  • 5
  • This is from client side. How to remove `b` in front of the message? `user@linux:~$ python client.py b'Thank you for connecting' user@linux:~$ ` –  Aug 18 '19 at 07:12
  • `b` specifies a byte string where each character in the string is a byte. If you only transfer ASCII string, you don't need to care about encoding and decoding. You may use `msg = received_msg.decode('ascii')` to convert it back to `str` type. – honglei Aug 18 '19 at 07:26