Python Forum
Can't strip extra characters from Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't strip extra characters from Data
#1
I'm new at Python with a Fortran, Basic and Visual Basic background, but I've managed to get a running program, but can't strip off the extra information.
This is the code:


import socket, sys

host = ''               
port = 65432
socksize = 19

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s" % port)
s.listen(1)
print("Now listening...n")


while True:
    conn, addr = s.accept()
    data = conn.recv(socksize)
    string = data.strip('/') #this line doesn't work
    if not data:             #it gives me: TypeError: a bytes-like object is required, not 'str'
        x=1
    else:
        print(data)
        print(string)
It gets the data when I don't have the string & print(string) lines in there, but I was trying to strip off the b'GET / on the beginning of the data and the ' on the end. Being new to Python, does it always return the b'' when there is no data coming? Also once you write a program in Python, can it be compiled in Python so that it's statements are not visible? That's kind of why I put the dummy statement x=1 when there was no data in order to not have a running string of b'' when there is no data being received.
Thanks!
Reply
#2
Coming from a socket it will always be bytes,can easily convert to string str type.
>>> data = b'/hello123/'
>>> type(data)
<class 'bytes'>
>>> 
>>> data = data.decode()
>>> type(data)
<class 'str'>
>>> data
'/hello123/'
>>> data.strip('/')
'hello123'
When the change from Python 2 to Python 3 happened they made a clear choice that bytes and string(Unicode) can not mix.
Can also do it like and only convert when needed.
>>> data = b'hello123/'
>>> type(data)
<class 'bytes'>
>>> data.strip(b'/')
b'hello123'
When convert to str it will be Unicode as all string in Python3 is that,
so in background is really doing this when use .decode()
data = data.decode('utf-8')
Reply
#3
I put the line data = data.decode('utf-8') in my program to see what it would do and I see that it takes off the b' ' and just returns the GET / and the data with the & in the middle since I'm getting 2 string variables. What's the easiest way to strip off the GET / and extract the 2 strings to then so that I can then format them in another string to send off to another computer? First variable 10 characters, 2nd variable 3 characters. Also what command do I use to send off the data to another computer? Do I have to open a secondary socket to send that info or is there just a simple send command that would do that?

Thanks!!! on the data.decode suggestion. I had tried that before my submission to the forum here, but didn't know about putting the utf-8 or even what that code means.
Reply
#4
Tried:
data = data.decode('utf-8')
        string= data.strip('/')
        print(data)
        print(string)
But, it didn't change anything in the output. I was thinking it would strip off the lead characters of GET / but didn't do anything. Both data and string showed the same info.
Reply
#5
Do print(repr(data)) then you see all content,post the result if trouble.
strip() only remove characters at the beginning and end of string.
Reply
#6
I managed to get this far on the code. It now works and strips out the information I want and presents it, but I'm now trying to figure out how to send the information received to another computer. I'm not sure how I'm supposed to send the information. I've tried a few different basic methods and now I'm stuck at this point: I tried creating a secondary socket figuring I had to in order to send the data forward to a different machine, but I've fought with getting different errors.

import socket, sys

host = ''               
port = 65432
socksize = 19

host2 = '192.168.1.228:65431'
socksize = 19

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
print("Now listening...")

def remove_punc(s):
    from_ = 'GET/&'
    to = '     '
    translation = str.maketrans(from_,to)
    new = s.translate(translation)
    return ' '.join(new.split())


while True:
    conn, addr = s.accept()
    data = conn.recv(socksize)
    if not data:             
        x=1
    else:
        host2.send(bytes("Hey there!!", "utf-8"))
        #host2.send("data")
        data = data.decode('utf-8')
        print(data)
        print(remove_punc(data))
Reply
#7
I'm not sure if I'm on the right path. The code above seems to be able to accept data from multiple IP addresses and reflect the information on screen as it comes in. Now that I've got the formatting correct to get the information displayed, I thought the next step would be to send/forward that information to another computer that is waiting to get that information. I'm not sure if there is an easy "send" method to forward the "punctuated" data to another computer on the network or if I have to create a second socket for that purpose which what I was attempting to do in the above code. I'm trying to keep it as simple and clean as I can..
Reply
#8
I've managed to figure out both how to clean up the data and strip off the extra characters as well as forward the info to another computer. Now I'm stuck as I'm not sure if I've got the right object to communicate with the other computer. It seems that sockets only transmit in binary and after stripping the information, I am unable to send the re-formatted in text format to the other computer. It will only go in binary with the extra characters and I can also not send a carriage return so that the data sent comes in on a new line with each send? Since I'm unfamiliar with Python and only have experience with older languages, is there a way to do this or do I have to utilize a different language?

I'll include the code where I'm at at this point so you can see what I've done and maybe someone will have a solution.

Thanks in advance!

import socket, sys

host = ''               
port = 65432
socksize = 19

s2 = socket.socket()
s2.connect(('192.168.1.228',65431))

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
print("Now listening...")

def remove_punc(s):
    from_ = 'GET/&'
    to = '     '
    translation = str.maketrans(from_,to)
    new = s.translate(translation)
    return ' '.join(new.split())


while True:
    conn, addr = s.accept()
    data = conn.recv(socksize)
    if not data:             
        x=1
    else:
        s2.send(data)
        #s2.send(0D0A HEX)
        data = data.decode('utf-8')
        print(data)
        test = (remove_punc(data))
        print(test)
        #s2.send(test)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,922 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,194 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  strip() pprod 8 3,451 Feb-16-2021, 01:11 PM
Last Post: buran
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,677 May-15-2020, 01:37 PM
Last Post: snippsat
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,758 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,850 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,579 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,323 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,878 Apr-15-2019, 03:14 AM
Last Post: ineuw
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,447 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020