I'm fairly new to Python. I have two scripts running that are communicating with each other, but once the sender process stops sending bytes, the receiver process receives an endless stream of what decodes (UTF-8) to new lines. I've reduced the code as much as I could to keep things simple:
Sender Python script.
import socket
s = socket.socket()
host = "127.0.0.1"
port = 5409
s.bind((host, port))
data_to_send = ['1','2','3','4','5','6','7','8','9']
s.listen(1)
c, addr = s.accept()
print ('Got connection from ', addr,'. Sending data...', sep='')
for data in data_to_send:
message = data.encode('utf-8')
c.sendall(message)
Receiver Python script.
import socket
messages_received = 0
s = socket.socket()
host = "127.0.0.1"
port = 5409
s.connect((host, port))
while True:
incoming_message = s.recv(1024).decode('utf-8')
messages_received += 1
# This condition is just to avoid printing thousands of lines
if messages_received < 10:
print(messages_received, ':', incoming_message)
Receiver output.
1 : 1
2 : 23456789
3 :
4 :
5 :
6 :
7 :
8 :
9 :
What am I doing wrong? I would ideally want the sender script to break out of the "While True" loop if the socket closes.
As #jasonharper pointed out, all I needed to do was to check for empty messages and break the loop as soon as that happens. When the sender doesn't send anything, the receiver doesn't receive empty massages, it just waits for a valid message, which I didn't know. The following code worked for me:
Sender Python script.
import socket
import time
s = socket.socket()
host = "127.0.0.1"
port = 5409
s.bind((host, port))
data_to_send = ['1','2','3','4','5','6','7','8','9']
s.listen(1)
c, addr = s.accept()
print ('Got connection from ', addr,'. Sending data...', sep='')
for data in data_to_send:
message = data.encode('utf-8')
c.sendall(message)
time.sleep(1)
Receiver Python script.
import socket
messages_received = 0
s = socket.socket()
host = "127.0.0.1"
port = 5409
s.connect((host, port))
while True:
incoming_message = s.recv(1024).decode('utf-8')
messages_received += 1
if not incoming_message:
break
if messages_received < 10:
print(messages_received, ':', incoming_message)
Receiver output.
1 : 1
2 : 2
3 : 3
4 : 4
5 : 5
6 : 6
7 : 7
8 : 8
9 : 9
well you can try setting the buffer size on sender side :
socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024) # Buffer size 1024
if it dosent work you can try even dict format so you send data as json format.
Related
I try to make a two udp listening servers in one python code code is below,
import threading
import time
import socket
class udpreceive:
def __init__(self,port,ip):
self.port = port
self.ip = ip
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((self.ip, self.port))
def startserver(self):
while True:
time.sleep(1)
data, addr = self.sock.recvfrom(1024)
print (data)
server1 = udpreceive(514,"192.168.1.5")
s1 = threading.Thread(target=server1.startserver())
server2 = udpreceive(515,"192.168.1.5")
s2 = threading.Thread(target=server2.startserver())
s2.start()
s1.start()
this is client code for udp sender 1
import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
counter =0
while True:
send_data = "hellow world server1 :- "+ str(counter)
sock.sendto(send_data.encode(), ("192.168.1.5", 514))
print (send_data)
counter +=1
this is client code for sender 2
import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
counter =0
while True:
send_data = "hellow world server2 :- "+ str(counter)
sock.sendto(send_data.encode(), ("192.168.1.5", 514))
print (send_data)
counter +=1
When i run receive code its only show sender 1 data. start only sender 2 and start receive code but its not show any sender 2 sending data but i start sender 1 receive code show sender 1 data.
what is the wrong thing i do? i need to show both sender 1 and sender 2 data in receive program
im new to oop and socket programming
thanks
In threading module, "target" keyword argument should not contain brackets,
see How NOT to wait for a thread to finish in Python . As it should be:
threading.Thread(target=server1.startserver)
Then, the two UDP server threads shall start then join, as fairly sharing CPU resources is important (especially when using infinite loops).
The code for the server could be:
import threading
import time
import socket
class udpreceive:
def __init__(self,port,ip):
self.port = port
self.ip = ip
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((self.ip, self.port))
def startserver(self):
while True:
data, addr = self.sock.recvfrom(1024)
print(f'port {self.port} receives {data}')
s1 = udpreceive(514, "192.168.1.5")
s2 = udpreceive(515, "192.168.1.5")
threads = [threading.Thread(target=s1.startserver), threading.Thread(target=s2.startserver)]
for th in threads:
th.start()
print(f'threads {th} started')
th.join(0.1)
Meanwhile, you may need to change the code in sender 2 to:
sock.sendto(send_data.encode(), ("192.168.1.5", 515)) # Send to port 515, not 514
my raspberry pi is the server and Im trying to send continuous message from rpi to android while recieving a command from client (android app),i really dont know if this is possible and how to do it is out of my reach and it is not a feedback message here is my code hope you will help me thank you.
import apptopi
from socket import *
from time import ctime
from nanpy import (ArduinoApi, SerialManager)
apptopi.setup()
connection = SerialManager()
a = ArduinoApi(connection = connection)
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
add = 0
add += 1
a = str(add) //**this is a sample that i want to send continously
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(0)
tcpSerSock.send(str.encode(a)) <== i really don't know how to send
continuously
while True:
print 'Waiting for connection'
tcpCliSock,addr = tcpSerSock.accept()
print '...connected from :', addr
try:
while True:
data = ''
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
if data == ctrCmd[0]:
apptopi.forw()
print 'forward'
if data == ctrCmd[1]:
apptopi.back()
print 'backward'
if data == ctrCmd[2]:
apptopi.left()
print 'leftturn'
if data == ctrCmd[3]:
apptopi.right()
print 'rightturn'
if data == ctrCmd[4]:
apptopi.stp()
print 'stop'
except KeyboardInterrupt:
apptopi.close()
GPIO.cleanup()
tcpSerSock.close();
OK one approach is to use the select() function for this. There is information in the documentation about its operation.
As an example I've made a modified version of your program (see below). I don't have a raspberry pi, so that part of the code is commented out, but you can replace it as needed.
The example uses the timeout feature of select() to send "continuous" messages to clients whilst also monitoring them for incoming messages. You can adjust the message contents and timeout to whatever works for you. NB you may also need to respond to client messages, as this code only sends data to clients after a timeout. Make whatever changes you need.
import sys
import socket
import select
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(1)
print 'Waiting for connection'
sendInterval = 1.0 # interval(sec) for sending messages to connected clients
rxset = [tcpSerSock]
txset = []
while 1:
rxfds, txfds, exfds = select.select(rxset, txset, rxset, sendInterval)
if rxfds:
for sock in rxfds:
if sock is tcpSerSock:
# a client is connecting
tcpCliSock, addr = tcpSerSock.accept()
tcpCliSock.setblocking(0)
rxset.append(tcpCliSock)
print '...connected from :', addr
else:
# a client socket has data or has closed the connection
try:
data = sock.recv(BUFSIZE)
if not data:
print "...connection closed by remote end"
rxset.remove(sock)
sock.close()
else:
if data == ctrCmd[0]:
#apptopi.forw()
print 'forward'
if data == ctrCmd[1]:
#apptopi.back()
print 'backward'
if data == ctrCmd[2]:
#apptopi.left()
print 'leftturn'
if data == ctrCmd[3]:
#apptopi.right()
print 'rightturn'
if data == ctrCmd[4]:
#apptopi.stp()
print 'stop'
except:
print "...connection closed by remote end"
rxset.remove(sock)
sock.close()
else:
# timeout - send data to any active client
for sock in rxset:
if sock is not tcpSerSock:
sock.send("Hello!\n")
The simple client program I used to test this is here:
import sys
import socket
import time
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
HOST = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)
time.sleep(1)
for i in range(len(ctrCmd)):
tcpCliSock.send(ctrCmd[i])
time.sleep(1)
data = tcpCliSock.recv(BUFSIZE)
print data
tcpCliSock.close()
Hope this helps, best of luck.
I'm trying to find the sum of the two numbers. These are my codes for server and client, the result I want is
CLIENT RECEIVED : Sum 46
but instead I get
CLIENT RECEIVED : Sum 12
CLIENT RECEIVED : Sum 34
Client:
import socket
def parse_file(fName):
list = []
with open(fName) as f:
for line in 1f:
list.append(line.strip())
return list
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
input_data = parse_file('foo')
for i in input_data:
s.sendall(str(i))
sum = s.recv(1024)
print "CLIENT RECEIVED : Sum ", sum
s.close()
Server:
import socket
def compute_sum(line):
result = sum(int(i) for i in line)
return result
HOST = 'localhost' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
print "SERVER RECIEVED : ", repr(data)
compute_sum(data)
conn.sendall(data)
conn.close()
foo
23
12
Your print statement is inside your loop, and sum is being set to the new value each time through, rather than incremented by the appropriate amount. Try this instead:
sum = 0
for i in input_data:
s.sendall(str(i))
sum += int(s.recv(1024))
print "CLIENT RECEIVED : Sum ", sum
Trying to make a server that tells the client what number he/she is. For example, once you connect it should say something like "Welcome client #5" or something along those lines. Right now I'm just trying to write it so that it simply reads a line in and echos it back. Im stuck on as far as getting it to show the clients number.
import socket
import sys
host = ''
port = 37373
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
while 1:
s, address = s.accept()
data = s.recv(1024)
if data:
s.send(data)
s.close()
that is
import socket
import sys
Clinet_number = 0
host = ''
port = 37373
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(10) # number of queued connections
while 1:
Client_number += 1
s, address = s.accept()
data = s.recv(1024)
if data:
s.send(str(Client_number))
s.close()
I have been looking at some code for a small chat program that I found online. It was originally written for 2.7, but it seems to work with 3.2. The only problem is that I cannot send strings, only numbers:
The chat.py file source code:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(input("Reply: "))
conn.send(reply)
conn.close()
And the client.py source file:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()
When I run these using two separate terminals, they work, but do not send strings.
Thank you
When you work with sockets, the message you're passing around should probably be in bytes, b'bytes'. In Python 2.x, a str is actually what a bytes is in Python 3.x
So your message should be something like:
message = b'Message I want to pass'
Check here http://docs.python.org/3.3/library/stdtypes.html for more information.
According to http://docs.python.org/3/library/functions.html#input input returns a str, which means you'll have to encode message into bytes as such:
message = message.encode()
Do verify that this is the right approach to convert str to bytes by checking the type of message.
Your socket code is correct, it was just failing due to an unrelated error due to raw_input vs input. You probably intended to read a string from the shell instead of reading a string and trying to evaluate it as Python code which is what input does.
Try this instead:
chat.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(raw_input("Reply: "))
conn.send(reply)
conn.close()
client.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(raw_input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()