How to send a message from client to server in python
Asked Answered
S

2

7

I'm reading two programs in Python 2.7.10 with client and server. How can I modify these programs in order to send a message from client to server?

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 = 80              # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done
Suisse answered 14/5, 2016 at 13:20 Comment(0)
S
13

TCP sockets are bi-directional. So, after connection, there is no difference between server and client, you only have two ends of a stream:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
s.bind(('0.0.0.0', 12345))        # 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
   print c.recv(1024)
   c.close()                # Close the connection

and the client:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
s.connect(('localhost', 12345))
s.sendall('Here I am!')
s.close()                     # Close the socket when done
Shamblin answered 14/5, 2016 at 13:36 Comment(0)
F
8

The above answer throws an error: TypeError: a bytes-like object is required, not 'str' However, the following code worked for me:

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 3125
s.bind(('0.0.0.0', port))
print ('Socket binded to port 3125')
s.listen(3)
print ('socket is listening')

while True:
    c, addr = s.accept()
    print ('Got connection from ', addr)
    print (c.recv(1024))
    c.close()

client.py:

import socket

s = socket.socket()
port = 3125
s.connect(('localhost', port))
z = 'Your string'
s.sendall(z.encode())    
s.close()
Facesaving answered 29/11, 2018 at 7:43 Comment(1)
The reason why it provided the error was because of some new features introduced in newer python versions. So, you need to convert your string into bytes and then decode bytes into string. message = 'Hello World' string_to_bytes = bytes(message, encoding = 'utf-8') afterwards, your message is received in the other end (as bytes_message) and you convert it to string by bytes_to_string = str(bytes_message, encoding = 'utf-8') str() is pretty powerful :)Tannin

© 2022 - 2024 — McMap. All rights reserved.