python socket beginner error - python

I just started coding in python I can't the encryption part of strings
I am trying to run this simple server client code
(The client is to run on raspberry-pi)
server :
#!/usr/bin/env python
import socket
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
BUFFER_SIZE = 24
conn, addr = server_socket.accept()
print ('Got connection from', addr)
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print ("received data:", data)
conn.send(data) # echo
conn.close()
Client:(were I have the error)
import socket
client_socket = socket.socket()
client_socket.connect(("192.168.1.4", 8000))
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
client_socket.send(MESSAGE)
data = client_socket.recv(BUFFER_SIZE)
client_socket.close()
print ("received data:", data)
The error here =:
File"c.py" line 9, in <module>
client_socket.send<MESSAGE>
typeError:'str' does not support the buffer interface

In python3 the interface to socket.send() changed to accept bytes instead of a string. See the difference between Python 3 docuentation and Python 2 documenation.
The solution is to encode the string before passing it to send() as follows:
client_socket.send(MESSAGE.encode())

In Python 3x strings are unicode, and they have to be encoded to bytes to send to a socket. The line:
client_socket.send(MESSAGE)
needs to be changed to:
client_socket.send(MESSAGE.encode('utf-8'))
On the server side you can decode data to get a string.

Related

WinError 10057 - Python TCP Server is not receiving client connections from localhost

I have this python server code here, which is waiting to receive a message digest and an encrypted message from a python client.
Clientside socket:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s=socket.socket()
s.connect((HOST, PORT))
s.sendall(transmit)
Server Side:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print("\n Server is listing on port :", PORT, "\n")
fragments = []
#Execution stops here
with conn:
print(f"Connected by {addr}")
while True:
chunk = s.recv(4096)
if not chunk:
break
fragments.append(chunk)
arr = 'b'.join(fragments)
#Test to see if the array was being populated
print(arr[0])
I have tried the methods this stackOF post here, specifically above is the provided list method implementation as my client is sending a "packet" of information as a list encoded as a string
packet = [signeddigest, ciphertext]
transmit = str(packet)
transmit = transmit.encode()
s.sendall(transmit)
I have tested my client code on a different server codebase with the same localhost and port number, and that server was receiving the information, so there's something I'm missing in the server side.
The output from the test server was
File [b'HT\xb0\x00~f\xde\xc8G)\xaf*\xcc\x90\xac\xca\x124\x7f\xa0\xaa\ requested from ('127.0.0.1', 49817)
That "file" is the encoded string sent from my client to the test server. So I'm confident there's something wrong with my server implementation.
Further information:
When I run the server it listens, then I run the client.
python ClientTest.py
Please enter the message to send
Then the server side immediately closes the connection
line 23, in
chunk = s.recv(4096) OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and
(when sending on a datagram socket using a sendto call) no address was
supplied
You have a number of inconsistencies in your code:
while True:
chunk = s.recv(4096) # should be conn.recv(4096)
if not chunk:
break
fragments.append(chunk) # misaligned: you only append the empty chunk
arr = 'b'.join(fragments) # 'b' is an unicode string. You want b''
After fixing that to:
while True:
chunk = conn.recv(4096)
if not chunk:
break
fragments.append(chunk)
arr = b''.join(fragments)
arr will receive the sent data as soon as the client uses close or shutdown on its side of the socket.
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
conn is the connected socket, s is the listener socket.
chunk = s.recv(4096)
The error you get is because you are trying to read from the listener socket s, not from the connected socket conn:
line 23, in chunk = s.recv(4096) ... A request to send or receive data was disallowed because the socket is not connected ...

Python decode TCP from LabVIEW

I am trying to decode data coming via TCP from labview. I want to send 4-bit header denoting the length followed by the message.
My LabVIEW sucessfully send the 4-bytes to the Python. And Python recieves b'\x00\x00\x00\x016' which clearly denotes a byte of information.
How do I then decode the command back to the integer length such that I can have an additional read to read in the rest of the message?
This is my python script:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 9991))
server.listen(1)
while True:
conn, addr = server.accept()
messagelen = conn.recv(4)
print(messagelen)
bits = bytes.decode(messagelen)
print(bits)
server.close()
LabVIEW script
Are you sure your bytes you receive aren't b'\x00\x00\x00\x16'? If they are you can use the struct module to unpack.
import socket
import struct
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 9991))
server.listen(1)
while True:
conn, addr = server.accept()
bits = conn.recv(4)
messagelen = struct.unpack('>i', bits)[0]
print(messagelen)
server.close()

Socket Python read bynary data

I am getting information from a socket via UDP.
I do not know how to read, this type of date below is binary, hexadecimal ???
��=~%01.00 EB200318151C0000003s��Z�t|
This is the result of part:
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
Can someone help, what kind of information is coming, how should I read it?
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
data, addr = s.recv(1024)
print "received message:" + data

Python 3.x Socket Module: "bytes like object not str"

I am in the process of making a simple networking messaging interface, using the Sockets module preinstalled in Python 3.5.1 . The code returns with a
TypeError: a bytes-like object is required, not 'str'
The server receives the message, but after that the program crashes with that error^I have done research and am aware of using .encode/.decode('utf-8') but it still returns the same error. The error is on the tcpServer.py file, whose source code will be below. I've read about using b'string here' but i don't know if it is applicable to variables. Thank you in advance.
Source:
import socket
def Main():
host = "127.0.0.1" #makes localhost the host server
port = 5000 #any random port between 1024 and 65535
s = socket.socket() #creates a new socket object
s.bind((host, port))
s.listen(1) #listens for 1 connection
c, addr = s.accept() #accepts a connection and address
print("Connection from: ", str(addr))
while True:
data = c.recv(1024) #receives bytes from connection with a 1024 buffer
if not data:
break
print("From Client: ",str(data.decode()))
data = str(data).upper() #overwrites current values in var data with a string of the new data in uppercase
print("Sending: ",str(data))
c.send(data) #tells connection to send the data
c.close()
if __name__ == "__main__":
Main()

My simple client crash everytime it tries to connect to my python socket server

My client crash everytime it tries to connect to my python socket server. I dont know why but but my server seams to start up fine then when i start up my client it establishes a connection to the server but it crash direct. Im doing almost as what they say in py socket docs so im wondering if i have missed something or just staring blindly on something easy. Could any one help pls im using python3.4.
server.py
import socket
host = ''
port = 1010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print ("Connection from", addr)
while True:
databytes = conn.recv(1024)
if not databytes: break
print("Recieved: "+(databytes.decode('utf-8')))
response = input("Reply: ")
if response == "exit":
break
conn.sendall(response.encode('utf-8'))
conn.close()
client.py
import socket
host = '127.0.0.1'
port = 1010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print("Connected to "+(host)+" on port "+str(port))
initialMessage = input("Send: ")
s.sendall(initialMessage.encode('utf-8'))
while True:
data = s.recv(1024)
print("Recieved: "+(data.decode('utf-8')))
response = input("Reply: ")
if response == "exit":
break
s.sendall(response.encode('utf-8'))
s.close()
There are two issues in your server/client programs.
You server has two accept() calls, before the while loop and inside the while loop , this causes the server to actually wait for two connections before it can start receiving any messages from any client.
The socket.sendall() function takes in bytes not string , so you need to encode the string using some suitable encoding to convert it to bytes , before sending the data . Also , the function - socket.recv() - returns bytes . So you need to decode the bytes data (using the same encoding you used to encode when sending) , so that you can get the correct string back and it can be printed. A suitable encoding can be - utf-8 - but you can use any encoding of you choice.
An example of encoding a string and using sendall() func -
s.sendall(response.encode('utf-8'))
an example of decoding the string when receiving the data -
databytes = conn.recv(1024)
if not databytes: break
data = databytes.decode('utf-8')
You do not neccessarily need to store the decoded value in another variable, you can also directly call the .decode('utf-8') function when printing the data like - print("Recieved: "+(data.decode('utf-8'))) .

Categories

Resources