String modifying error, trying to delete data in string

M

mikie

Here is some code:
import socket,sys

s=socket.socket()
port=int(sys.argv[1])
s.bind(("127.0.0.1",port))
s.listen(2)
cls,addr=s.accept()

data=cls.recv(1024)
f=data.pop("Proxy-Connection")
cls.close()

Im trying to delete some headers in the request but get an error saying:
'str' object has no attribute 'pop'

How can I modify data received from the client?
 
M

Mark Lawrence

Here is some code:
import socket,sys

s=socket.socket()
port=int(sys.argv[1])
s.bind(("127.0.0.1",port))
s.listen(2)
cls,addr=s.accept()

data=cls.recv(1024)
f=data.pop("Proxy-Connection")
cls.close()

Im trying to delete some headers in the request but get an error saying:
'str' object has no attribute 'pop'

How can I modify data received from the client?

Strings are immutable so you'll need string methods to manipulate your
data see
http://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
 
S

Steven D'Aprano

Im trying to delete some headers in the request but get an error saying:
'str' object has no attribute 'pop'

What sort of object is data? What do you expect it to be, and what it is
actually?

Given that you are calling data.pop("Proxy-Connection"), that suggests to
me that you think data is a dict. The error message given tells you that
data is a str.
How can I modify data received from the client?

The same as any string: make a copy with whatever modifications you want.
Strings have a replace method; you can use the re module to use regexes;
you can slice them with some_string[start:end] syntax, etc. But it looks
to me like you might be better off using a higher-level protocol rather
than using socket. What sort of data are you expecting back?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top