I have pretty straightforward Python code which queries data from memcached:
import memcache
client = memcache.Client([('127.0.0.1', 11211)])
res = client.get("data_e")
data_e key exists, is not null and I am able to get results via telnet connection to memcached.
However when Python script is executed I keep receiving following error:
Traceback (most recent call last):
File "./position_expiration.py", line 5, in <module>
res = client.get("data_e")
File "/usr/lib/python2.7/site-packages/memcache.py", line 1129, in get
return self._get('get', key)
File "/usr/lib/python2.7/site-packages/memcache.py", line 1113, in _get
return _unsafe_get()
File "/usr/lib/python2.7/site-packages/memcache.py", line 1101, in _unsafe_get
value = self._recv_value(server, flags, rlen)
File "/usr/lib/python2.7/site-packages/memcache.py", line 1294, in _recv_value
raise ValueError('Unknown flags on get: %x' % flags)
ValueError: Unknown flags on get: 20
Data in memcached is stored by third party service, so I can't change a way how it is written there. What can I do from client side with Python to read it?
I have solved this problem.
This is the solution in Chinese: https://www.cnblogs.com/LanTianYou/p/9204431.html
The solution is to modify the final else-block content in the method "_recv_value" in the python file "memcache.py".
The _recv_value method should be like this:
def _recv_value(self, server, flags, rlen):
rlen += 2 # include \r\n
buf = server.recv(rlen)
if len(buf) != rlen:
raise _Error("received %d bytes when expecting %d"
% (len(buf), rlen))
if len(buf) == rlen:
buf = buf[:-2] # strip \r\n
if flags & Client._FLAG_COMPRESSED:
buf = self.decompressor(buf)
flags &= ~Client._FLAG_COMPRESSED
if flags == 0:
# Bare bytes
val = buf
elif flags & Client._FLAG_TEXT:
val = buf.decode('utf-8')
elif flags & Client._FLAG_INTEGER:
val = int(buf)
elif flags & Client._FLAG_LONG:
if six.PY3:
val = int(buf)
else:
val = long(buf) # noqa: F821
elif flags & Client._FLAG_PICKLE:
try:
file = BytesIO(buf)
unpickler = self.unpickler(file)
if self.persistent_load:
unpickler.persistent_load = self.persistent_load
val = unpickler.load()
except Exception as e:
self.debuglog('Pickle error: %s\n' % e)
return None
else:
self.debuglog("unknown flags on get: %x\n" % flags)
# 注释掉这行
# raise ValueError('Unknown flags on get: %x' % flags)
# 设定返回值
val = buf
return val
Related
I am new to python, I am creating a websocket with pyhton as server and javascript as client, I got them to communicate but I cannot pass very extensive data because it throws me this error when the python function receives and tries to decode the bytes and the PAYLOAD_LENGTH.
This is my function that treats the received bytes
def parse_frame(self):
data = self.conn.recv(2)
FIN = (data[0] >> 7) & 1
RSV1 = (data[0] >> 6) & 1
RSV2 = (data[0] >> 5) & 1
RSV3 = (data[0] >> 4) & 1
OPCODE = data[0] & 0xf
MASK = (data[1] >> 7) & 1
if not MASK:
self.close_handshake(1002)
#print(data[1])
PAYLOAD_LENGTH = data[1] & 0x7f
#print(PAYLOAD_LENGTH)
if PAYLOAD_LENGTH == 126:
data = self.conn.recv(2)
PAYLOAD_LENGTH = struct.unpack('B', data)[0]
elif PAYLOAD_LENGTH == 127:
data = self.conn.recv(8)
print(data)
PAYLOAD_LENGTH += struct.unpack('B', data)[0]
MASKING_KEY = self.conn.recv(4)
PAYLOAD_DATA = self.conn.recv(PAYLOAD_LENGTH)
PAYLOAD_DATA = self.unmask(PAYLOAD_DATA, MASKING_KEY)
return OPCODE, PAYLOAD_DATA
Waiting connection at port 5000
1
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\digit\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 359, in run
self.data_transfer()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 226, in data_transfer
OPCODE, PAYLOAD_DATA = self.parse_frame()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 276, in parse_frame
PAYLOAD_LENGTH = struct.unpack('B', data)[0]
struct.error: unpack requires a buffer of 1 bytes
Can someone tell me how to solve this error?
I have the following code which returns the public IP's
def gather_public_ip():
ACCESS_KEY = config.get('aws','access_key')
SECRET_KEY = config.get('aws','secret_key')
regions = regions = ['us-west-2','eu-central-1','ap-southeast-1']
# regions = config.get('aws','region').split(',')
all_EIP = []
for region in regions:
client = boto3.client('ec2',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,region_name=region,)
addresses_dict = client.describe_addresses()
for eip_dict in addresses_dict['Addresses']:
if 'PrivateIpAddress' in eip_dict:
print eip_dict['PublicIp']
# return str(eip_dict['PublicIp'])
all_EIP.append(eip_dict['PublicIp'])
print all_EIP
# print str(all_EIP)
return str(all_EIP)
This is called and returned as :
net_range = gather_public_ip()
for ip in net_range:
r = s.run(ip)
run looks like :
def run(self, targets="" ,options="-Pn"):
#start a new nmap scan on localhost with some specific options
syslog.syslog("Scan started")
parsed = None
nmproc = NmapProcess(targets,options)
rc = nmproc.run()
if rc != 0:
syslog.syslog("nmap scan failed: {0}".format(nmproc.stderr))
try:
parsed = NmapParser.parse(nmproc.stdout)
self.report = parsed
except NmapParserException as e:
syslog.syslog("Exception raised while parsing scan: {0}".format(e.msg))
syslog.syslog("Scan complete")
syslog.syslog("Scan duration: "+ str(parsed.elapsed))
self.report = parsed
return parsed
after printing the list , this throws me :
Traceback (most recent call last):
File "portwatch.py", line 300, in <module>
r = s.run(ip)
File "portwatch.py", line 239, in run
rc = nmproc.run()
File "/usr/local/lib/python2.7/dist-packages/libnmap/process.py", line 257, in run
else shlex.split(self.__nmap_command_line)
File "/usr/lib/python2.7/shlex.py", line 279, in split
return list(lex)
File "/usr/lib/python2.7/shlex.py", line 269, in next
token = self.get_token()
File "/usr/lib/python2.7/shlex.py", line 96, in get_token
raw = self.read_token()
File "/usr/lib/python2.7/shlex.py", line 172, in read_token
raise ValueError, "No closing quotation"
ValueError: No closing quotation
Make sure your ip is not "" or shlex will fail, cf Which exception to raise if a given string does not match some format?
I want to retrive content from sent messages folder by the code below:
conn = imaplib.IMAP4_SSL('imap.exmail.qq.com',993)
conn.login(user,pwd)
conn.select("Sent Messages")
mails = conn.search(None, 'ALL')
for num in mails[1][0].split():
t, d = conn.fetch(num, 'RFC822')
if t == 'OK':
print 'Message %s\n' % (num)
msg = email.message_from_string(d[0][1])
subject = email.Header.decode_header(msg['subject'])[0][0]
if re.search(month, subject):
print subject
else:
print 'fetch error'
The num is between 1~36, when the num is 8. There comes an error:
Traceback (most recent call last):
File "monthlySummary.py", line 30, in
t, d = conn.fetch(num, 'RFC822')
File "/usr/lib/python2.7/imaplib.py", line 455, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/usr/lib/python2.7/imaplib.py", line 1087, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib/python2.7/imaplib.py", line 911, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: FETCH => socket error: unterminated line
I try to relogin, but still come the same error. what does this socket error mean? how can i solve this problem?
Thanks.
From the imaplib source code (here), the relevant function is:
def _get_line(self):
line = self.readline()
if not line:
raise self.abort('socket error: EOF')
# Protocol mandates all lines terminated by CRLF
if not line.endswith(b'\r\n'):
raise self.abort('socket error: unterminated line')
line = line[:-2]
if __debug__:
if self.debug >= 4:
self._mesg('< %r' % line)
else:
self._log('< %r' % line)
return line
The comment in the function says that all lines must be terminated by CRLF, so it is obviously hitting a line that is not and so it aborts.
You need to make sure each line ends with '\r\n', so to do that you could simply add this string to each line if it is not already there, before processing it. This will make sure that this error upon which the program is aborted is not raised.
Could anyone tell me what am I doing wrong? I keep getting an error with this code.
I'm trying to download all of the swf's from primaryschoolgames just as an experiment but I can't seem to do it:
#!/usr/bin/env python
# encoding: utf-8
import sys, getopt
import os, urllib, urllib2, re, string, math
help_message = '''
'''
no_param = '''
'''
verbose = False
fakeMode = False
curPath = os.getcwd() + "/"
urlRegex = ''
FileRegex = ''
outputPath = ''
currentFile = ''
def removeDuplicates(seq):
# Not order preserving
keys = {}
for e in seq:
keys[e] = 1
return keys.keys()
def go(filename):
print "Having a look at " + string.capwords(filename)
global urlRegex, FileRegex, outputPath, currentFile
url = 'http://cdn.primarygames.com' + filename
urlRegex = '/'+filename+'/.+/download'
FileRegex = '/'+filename+'/(.*?)/download'
outputPath = curPath+"Swfs"+"/"
if not os.path.exists(outputPath):
os.makedirs(outputPath)
filelist = []
while(len(url)):
# looping system
newlist, url = scrapePage(url, filename)
filelist.extend(newlist)
print 'Found %s Files.' % len(filelist)
for swf in filelist:
swfurl = swf['url']
name = swf['name']
currentFile = name
#print 'Downloading '+name,
if not fakeMode:
#print ''
urllib.urlretrieve('http://cdn.primarygames.com' + swfurl, outputPath+name)
else:
print 'Not downloading %s.' % name
print "All done with %s!" % filename
def scrapePage(url, filename):
print 'Looking through '+url
html = urllib2.urlopen(url).read()
swflist = re.findall(urlRegex, html)
swflist = removeDuplicates(swflist)
swfs = []
for swfurl in swflist:
r = re.compile(FileRegex)
swfname = r.search(swfurl).group(1)
swfname = swfname.replace('-', ' ')
name = filename + "/" + swfname + ".swf"
name = string.capwords(name)
swf.append({'name':name,'url':swfurl})
r = re.compile(nextRegex)
result = r.search(html)
if result:
nextUrl = 'http://cdn.primarygames.com' + result.group(1)
else:
nextUrl = ''
return swfs, nextUrl
def main(argv=None):
global verbose, fakeMode
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "ho:vf", ["help", "output="])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option == "-v":
verbose = True
if option in ("-f", "--fake"):
fakeMode = True
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-o", "--output"):
output = value
if len(args):
swfs = args
else:
raise Usage(no_param)
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
if err.msg != help_message:
print >> sys.stderr, "\t for help use --help"
return 2
for swf in swfs:
go(swf)
if __name__ == "__main__":
sys.exit(main())
This is the error I keep getting:
Having a look at *
Looking through http://cdn.primarygames.com/*
Traceback (most recent call last):
File "C:\PrimarySchoolGames Swf Downloader.py"
, line 129, in <module>
sys.exit(main())
File "C:\PrimarySchoolGames Swf Downloader.py"
, line 125, in main
go(swf)
File "C:\PrimarySchoolGames Swf Downloader.py"
, line 48, in go
newlist, url = scrapePage(url, filename)
File "C:\Users\Terrii\Desktop\VB Extra's\PrimarySchoolGames Swf Downloader.py"
, line 67, in scrapePage
html = urllib2.urlopen(url).read()
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python27\lib\urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
A failed getaddrinfo normally suggests that something is wrong with the URL you're providing. Since I am able to resolve the address are you sure you aren't behind a proxy server? This could result in a failed DNS lookup which results in exactly this message.
How Python determines which proxy to use on Windows:
In a Windows environment, if no proxy environment variables are set,
proxy settings are obtained from the registry’s Internet Settings
section.
For more help I concurr with #MikeHunter. I tried to fix your code, but since I had to implement your Exception-Class to get the code running at all I think you should re-indent your code and provide more information. Sorry.
import cherrypy
import os
PORT = 8080
class intelServ:
def index(self, botkey = None, userkey = None, network = None, user = None, channel = None, msg = None, step = None, **args):
# If any necessary parameters are missing, the bot should fail silently.
#if not botkey or not userkey or not network or not user or not channel or not msg:
# return 'failing'
chatLog = Chat()
chatLog.genLogfile(botkey, userkey)
return "You said: " + msg
index.exposed = True
class Chat:
def genLogfile(self, botkey, userkey):
botHome = os.path.join(os.curdir, botkey)
chatLog = os.path.join(botHome, userkey)
print botHome
print chatLog
# check if bot has a home, if not make one
if not os.path.exists(botHome):
os.mkdir(botHome)
# check if communication is already in progress, if not start it.
if not os.path.exists(chatLog):
FILE = open(chatLog, "w")
FILE.close()
FILE = open(chatLog+".step","w")
FILE.close()
# configure and start our cherrypy server
cherrypy.config.update({'server.socket_port': PORT, 'server.socket_host': '0.0.0.0'})
cherrypy.quickstart(intelServ())
This code returns the following traceback and I cannot figure out why:
> 500 Internal Server Error
>
> The server encountered an unexpected
> condition which prevented it from
> fulfilling the request.
>
> Traceback (most recent call last):
> File
> "c:\python26\lib\site-packages\cherrypy\_cprequest.py",
> line 606, in respond
> cherrypy.response.body = self.handler() File
> "c:\python26\lib\site-packages\cherrypy\_cpdispatch.py",
> line 25, in __call__
> return self.callable(*self.args, **self.kwargs) File "D:\code\pycode\spam\chatbot.py",
> line 14, in index
> chatLog.genLogfile(botkey, userkey) File
> "D:\code\pycode\spampy\chatbot.py",
> line 22, in genLogfile
> botHome = os.path.join(os.curdir, botkey) File
> "c:\python26\lib\ntpath.py", line 73,
> in join
> elif isabs(b): File "c:\python26\lib\ntpath.py", line 57,
> in isabs
> s = splitdrive(s)[1] File "c:\python26\lib\ntpath.py", line 125,
> in splitdrive
> if p[1:2] == ':': TypeError: 'NoneType' object is unsubscriptable
>
> Powered by CherryPy 3.1.2
It looks like the parameter botkey as supplied to intelServ.index() is None (hence the 'NoneType'). You should uncomment your input validation code.