javascript - 为什么我的 python 代码不断收到 500 内部服务器错误?
问题描述
我正在编写一个应该是奇迹字符搜索引擎的代码,但是,我的 python 部分在被 javascript 调用时返回 500 错误。我知道 javascript 有效,因为我使用教授给我们的示例 python 脚本对其进行了测试,但我不知道为什么我的代码不起作用。它应该返回一个用逗号分隔不同值的字符串(字符的名称和指向其 wiki 的链接)。当我将 cgi.FieldStorage() 更改为简单输入并自行运行时,该代码有效,所以我不确定出了什么问题。
#!/usr/bin/env python3
# namelookup.py - Program to display name statistics
#!/usr/bin/env python
import cgi;
import cgitb
cgitb.enable()
from sortedcontainers import SortedDict
filePath="/home/class/SoftDev/namedata/"
def readfile(filename):
infile=open(filename,mode='r')
array=[]
for line in infile:
templine=line
array.append(templine.split(','))
infile.close()
return array[::-1]
def removePunctuation(s):
#Remove all punctuation from a string
import string
for c in string.punctuation:
s= s.replace(c,"")
return s
def createNameIndex(array):
#creates an index of every name and the number of the lines tha have that number on it
index={}
arraylen=len(array)
for x in range(arraylen):
allnames=removePunctuation(array[x][1]).lower().split(' ')
#catalogs each name, so "Peter Parker" will also appear wheyou search for just Peter or Parker. This has an addebenefit in that it allows the program to determine answermore relevant to the search, see the sortrelevancy function
for name in allnames:
if not name in index:
index[name]=[x]
else:
index[name].append(x)
return index
def createYearIndex(array):
#creates an index the same as the name index, except with years instead of names
index={}
arraylen=len(array)
for x in range(arraylen):
year=array[x][12][0:4]
if year in index:
index[year].append(x)
else:
index[year]=[x]
return index
def searchFor(term,nameindex,yearindex):
#searches the indexes for the desired term, keeping track oeach row that comes back, including duplicates, which wilbe used by sortRelevancy
allterms=term.split()
allterms.append(term)
allterms.append(term.replace(" ",''))
results=[]
for word in allterms:
if word in nameindex:
for number in nameindex[word]:
results.append(number)
if word in yearindex:
for number in yearindex[word]:
results.append(number)
return results
def print_header():
print ("""Content-type: text/html\n""")
def sortRelevancy(numberlist):
#counts the number of times that each answer has appeared, then sorts them in that order so that the name that appeared thmost times is at the top.
sortednumbers={}
for number in numberlist:
if not number in sortednumbers:
sortednumbers[number]=1
else:
sortednumbers[number]=sortednumbers[number]+1
finallist=sorted(sortednumbers.items(), key=lambda kv:(kv[1], [0]) )
finallist= finallist[::-1]
return finallist[0:10] # to prevent too many results, only th10 most relevant are shown
def main():
#main function, combines all the above part and supplies the interface for the user
filename=readfile ('/home/class/SoftDev/marvel/marvel-wikia-data.csv')
marvelindex=createNameIndex(filename)
yearindex=createYearIndex(filename)
form = cgi.FieldStorage()
searchforme=removePunctuation(form.getvalue("name").lower())
resultlist=searchFor(searchforme,marvelindex,yearindex)
listicle=sortRelevancy(resultlist)
results=""
print_header()
if listicle==[]:
print("No results found.")
else:
for x in listicle:
results+=filename[x[0]][1]+", "+filename[x[0]][2]+", "
print(results)
main()
解决方案
推荐阅读
- php - 无法从 magento 的导出页面下载产品 CSV 文件
- php - htaccess重写规则知识
- firebase - 使用 Firestore 作为数据库,如何在没有自由文本搜索的情况下向用户提供多个数据过滤器?
- r - R 用于搜索大型数据集的 for 循环的替代方案
- spring - Spring Kafka消费者线程池
- salesforce - 从 APEX SALESFORCE 映射和列出值
- javascript - tampermonkey JS 脚本中的 Dispatchevent 在第三次使用时不起作用
- .net - .Net Core 3.1 API 中的刷新令牌 Azure 广告身份验证
- tensorflow - 自定义DataGenerator tensorflow错误'ValueError:找不到可以处理输入的数据适配器'
- python-requests - 带有 python-requests 的 HTTP3 版本