0

Data coming to this tcp server is not write into file as well as on command line.

Error is as below,

Exception in thread Thread-10: Traceback (most recent call last): File "F:\Installation\Anaconda\lib\threading.py", line 916, in _bootstrap_inner self.run() File "", line 25, in run f.write(data) TypeError: write() argument must be str, not bytes

My code:

import socket
from threading import Thread
from socketserver import ThreadingMixIn
import time
TCP_IP = '192.168.0.159'
TCP_PORT = 9001
BUFFER_SIZE = 1024

class ClientThread(Thread):
    def __init__(self,ip,port,sock):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sock = sock
        print(" New thread started for "+ip+":"+str(port))

    def run(self):
        filename='ble_scan.txt'
        f = open(filename,'w')
        while True:
            data=self.sock.recv(BUFFER_SIZE)
            print((data))
            f.write(data)
            f.close()
            self.sock.close()
            break

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
    tcpsock.listen(5)
    print("Waiting for incoming connections...")
    (conn, (ip,port)) = tcpsock.accept()
    print('Got connection from ', (ip,port))
    newthread = ClientThread(ip,port,conn)
    newthread.start()
    threads.append(newthread)
for t in threads:
    t.join()

3
  • Cannot reproduce with this code. The most serious problem here is that tcpsock.listen(5) should be outside of the loop. And you can have race conditions if multiple threads try to write at the same time on the file. But this error is likely to come from a different version of your script. Commented Jun 28, 2019 at 11:24
  • Are you sure you're not trying to run this in python 2? Commented Jun 28, 2019 at 15:14
  • yes I'm sure @JamesKPolk Commented Jun 29, 2019 at 5:22

1 Answer 1

-1

test this f = open(filename,'w') or this f = open(filename,'a') instead of f = open(filename,'wb')

import socket
from threading import Thread
from socketserver import ThreadingMixIn
import time
TCP_IP = '192.168.0.159'
TCP_PORT = 9001
BUFFER_SIZE = 1024


class ClientThread(Thread):


    def __init__(self, ip, port, sock):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sock = sock
        print(" New thread started for "+ip+":"+str(port))


    def run(self):
        filename = 'ble_scan.txt'
        f = open(filename, 'w')
        while True:
            data = self.sock.recv(BUFFER_SIZE)
            print((data))
            f.write(data)
            f.close()
            self.sock.close()
            break


tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
    tcpsock.listen(5)
    print("Waiting for incoming connections...")
    (conn, (ip, port)) = tcpsock.accept()
    print('Got connection from ', (ip, port))
    newthread = ClientThread(ip, port, conn)
    newthread.start()
    threads.append(newthread)
for t in threads:
    t.join()

Sign up to request clarification or add additional context in comments.

2 Comments

This should be a comment, not an answer.
50 reps to comment -___-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.