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


Messages In This Thread
Can't strip extra characters from Data - by Canflyguy - Jan-09-2022, 04:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 3,081 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,265 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  strip() pprod 8 3,571 Feb-16-2021, 01:11 PM
Last Post: buran
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,989 May-15-2020, 01:37 PM
Last Post: snippsat
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,813 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,996 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,624 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,481 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,963 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,579 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