我在让基于 python 的不和谐机器人在我的 apache2 网络服务器上运行时遇到了困难。我正在使用 discord.py api 包装器,并使用 pyenv 安装了 python 3.5.1。
这是我的代码
#!/usr/bin/env python
import discord
import asyncio
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
client = discord.Client()
@client.event
async def on_message(message):
if message.content.startswith('!logi'):
Cmd = message.content.lstrip('!logi ')
Cmd = Cmd.split()
From = Cmd[0]
To = Cmd[2]
await client.send_message(message.channel, 'starting search for %s to %s' % (From, To))
To_High=['Hydrogen Fuel','Consumer Technology','Clothing','Domestic Appliances','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Synthetic Meat','Food Cartridges','Superconductors','Heliostatic Furnaces','Basic Medicines','Paladium','Silver','Gold','Indium','Lithium','Tantalum','Titanium','Uranium','Gallium','Lepidolite','Cobalt','Computer Components','Non-Lethal Weapons']
From_High=['Pesticides','Agricultural Medicines','Consumer Technology','Synthetic Meat','Progenitor Cells','Advanced Catalysers','Animal Monitors','Awuaponic Systems','Auto Fabricators','Bio Reducing Lichen','H.E. Suits','Resonating Separators','Robotics','Terrain Enrichment Systems','Non-Lethal Weapons','Biowaste','Scrap']
To_Industrial=['Hydrogen Fuel','Consumer Technology','Clothing','Domestic Appliances','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Synthetic Meat','Food Cartridges','Algae','Polymers','Semiconductors','Superconductors','Basic Medicines','Progenitor Cells','Gold','Copper','Aluminium','Beryllium','Indium','Lithium','Tantalum','Titanium','Uranium','Gallium','Auto Fabricators','H.E. Suits','Robotics','Leather','Natural Fabrics','Synthetic Fabrics','Non-Lehal Weapons']
From_Industrial=['Clothing','Domestic Appliances','Food Cartridges','Marine Supplies','Crop Harvesters','Mineral Extractors','Heliostatic Furnaces','Basic Medicines','Computer Components','Biowaste','Scrap']
To_Agri=['Pesticides','Agricultural Medicines','Hydrogen Fuel','Consumer Technology','Clothing','Domestic Appliances','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Marine Supplies','Crop Harvesters','Basic Medicines','Progenitor Cells','Animal Monitors','Aquaponic Systems','Terrain Enrichment Systems','Non-Lethal Weapons']
From_Agri=['Mineral Oil','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Algae','Leather','Natural Fabrics','Biowaste']
To_Refinery=['Hydrogen Fuel','Mineral Oil','Consumer Technology','Clothing','Domestic Appliances','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Synthetic Meat','Food Cartridges','Heliostatic Furnaces','Basic Medicines','Progenitor Cells','Gallite','Indite','Uranimite','Bertrandite','Coltan','Rutile','Advanced Catalysers','Resonating Separators','Non-Lethal Weapons','Scrap']
From_Refinery=['Hydrogen Fuel','Explosives','Polymers','Semiconductors','Superconductors','Copper','Aluminium','Beryllium','Indium','Lithium','Tantalum','Titanium','Uranium','Gallium','Lepidolite','Cobalt','Synthetic Fabrics','Biowaste']
To_Extraction=['Hydrogen Fuel','Explosives','Consumer Technology','Clothing','Domestic Appliances','Animal Meat','Coffee','Fish','Fruit','Grain','Tea','Synthetic Meat','Food Cartridges','Mineral Extractors','Basic Medicines','Bio Reducing Lichen','H.E. Suits','Non-Letha; Weapons']
From_Extraction=['Paladium','Silver','Gold','Gallite','Indite','Uranimite','Bertrandite','Coltan','Rutile','Biowaste']
if From == 'High-Tech':
Cmp1 = From_High
if From == 'Industrial':
Cmp1 = From_Industrial
if From == 'Agricultural':
Cmp1 = From_Agri
if From == 'Refinery':
Cmp1 = From_Refinery
if From == 'Extraction':
Cmp1 = From_Extraction
if To == 'High-Tech':
Cmp2 = To_High
if To == 'Industrial':
Cmp2 = To_Industrial
if To == 'Agricultural':
Cmp2 = To_Agri
if To == 'Refinery':
Cmp2 = To_Refinery
if To == 'Extraction':
Cmp2 = To_Extraction
for x in range(len(Cmp1)):
for y in range(len(Cmp2)):
if Cmp1[x] == Cmp2[y]:
await client.send_message(message.channel, Cmp1[x])
await client.send_message(message.channel, 'search finished')
if message.content.startswith('!fact'):
print ('received command')
await client.send_message(message.channel, 'Welcome to Factual-Bot v0.1.0! For all your trading needs!')
await client.send_message(message.channel, 'Use the !logi command to find profitable trade routes')
await client.send_message(message.channel, 'The syntax is !logi [economy1] to [economy2]')
await client.send_message(message.channel, 'The following economies are supported')
await client.send_message(message.channel, 'High-Tech')
await client.send_message(message.channel, 'Industrial')
await client.send_message(message.channel, 'Agricultural')
await client.send_message(message.channel, 'Refinery')
await client.send_message(message.channel, 'Extraction')
print ('transmission sent')
client.run('MTgyODU3ODMyOTM5NTIwMDAz.Ch9bIA.sse2NajDnizDGVxK6BqNIpPg_ng')
当我运行它时它运行得很好
python factual-bot.py
但它占据了外壳。我已经通过设置适当的“cgi-bin”脚本扩展名让我的网络服务器运行其他Python脚本,并且我将所有脚本的权限设置为755。尽管如此,这是 apache2 抛出的错误
[Fri May 20 00:15:37 2016] [error] [client 192.168.0.4] Premature end of script headers: factual-bot.py
File "/usr/lib/cgi-bin/factual-bot.py", line 12
async def on_message(message):
^
SyntaxError: invalid syntax
[Fri May 20 00:15:39 2016] [error] [client 192.168.0.4] Premature end of script headers: factual-bot.py
答案1
我做了更多研究,看来屏幕会更适合我的目的