我目前正在尝试使用Twisted库专门扭曲的单词来尝试与MSN进行交互.但是当我运行twisted提供的示例脚本时,我收到错误.具体来说,错误可以在这里找到http://i42.tinypic.com/wl945w.jpg.该脚本可以在http://twistedmatrix.com/projects/words/documentation/examples/msn_example.py找到.
平台是使用Python 2.6的Vista
编辑:完整输出:
Email (passport): mypassport@hotmail.com
Password: ******
2009-04-25 10:52:49-0300 [-] Log opened.
2009-04-25 10:52:49-0300 [-] Starting factory <twisted.internet.protocol.ClientFactory instance at 0x9d87e8c>
2009-04-25 10:52:55-0300 [Dispatch,client] Starting factory <twisted.words.protocols.msn.NotificationFactory instance at 0x9e28bcc>
2009-04-25 10:52:55-0300 [Dispatch,client] Stopping factory <twisted.internet.protocol.ClientFactory instance at 0x9d87e8c>
2009-04-25 10:52:55-0300 [Notification,client] Unhandled Error
Traceback (most recent call last):
File "/usr/local/lib/python2.5/site-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python2.5/site-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows 上构建一个使用 Pygame 的下载的 Python 应用程序。我已经安装了Python 2.5和Pygame 1.7.1。我是 Python 新手,但我只是尝试在 Windows 控制台命令行上输入顶级 .py 文件的名称。(我使用的是 Win XP Pro。)
这是我收到的消息。
C:\Python25\include\pygame\pygame.h(68) : 致命错误 C1083: 无法打开包含文件: 'SDL.h': 没有这样的文件或目录
我认为 Pygame 是构建在 SDL 之上的,不需要单独安装 SDL。尽管如此,我安装了 SDL 1.2.13 并将 SDL include 文件夹添加到我的 %INCLUDE% 环境变量中。还是没有运气。
我注意到C:\Python25\Lib\site-packages\pygame包含几个 SDL*.DLL 文件,但 python 树中的任何位置都没有 sdl.h 头文件。当然,我可以将 sdl 头文件复制到C:\Python25\include\pygame文件夹中,但这是一个令人厌恶的想法。
有人知道设置的正确方法吗?
编辑: 该应用程序是“企鹅机器”pygame 应用程序。
这是示例代码:
from mechanize import Browser
br = Browser()
page = br.open('http://hunters.tclans.ru/news.php?readmore=2')
br.form = br.forms().next()
print br.form
Run Code Online (Sandbox Code Playgroud)
问题是服务器返回不正确的编码(windows-cp1251).如何在mechanize中手动设置当前页面的编码?
错误:
Traceback (most recent call last):
File "/tmp/stackoverflow.py", line 5, in <module>
br.form = br.forms().next()
File "/usr/local/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 426, in forms
return self._factory.forms()
File "/usr/local/lib/python2.6/dist-packages/mechanize/_html.py", line 559, in forms
self._forms_factory.forms())
File "/usr/local/lib/python2.6/dist-packages/mechanize/_html.py", line 225, in forms
_urlunparse=_rfc3986.urlunsplit,
File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 967, in ParseResponseEx
_urlunparse=_urlunparse,
File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 1104, in _ParseFileEx
fp.feed(data)
File "/usr/local/lib/python2.6/dist-packages/ClientForm.py", line 870, in feed
sgmllib.SGMLParser.feed(self, data)
File "/usr/lib/python2.6/sgmllib.py", line 104, …Run Code Online (Sandbox Code Playgroud) 我是lxml的新手,对python来说很新,无法找到以下解决方案:
我需要从第3行导入一些包含3列和未定义行数的表.
当任何行的第二列为空时,将丢弃此行并中止对该表的处理.
以下代码打印表的数据很好(但之后我无法重用数据):
from lxml.html import parse
def process_row(row):
for cell in row.xpath('./td'):
print cell.text_content()
yield cell.text_content()
def process_table(table):
return [process_row(row) for row in table.xpath('./tr')]
doc = parse(url).getroot()
tbl = doc.xpath("/html//table[2]")[0]
data = process_table(tbl)
Run Code Online (Sandbox Code Playgroud)
这只打印第一列:(
for i in data:
print i.next()
Run Code Online (Sandbox Code Playgroud)
以下仅导入第三行,而不是后续行
tbl = doc.xpath("//body/table[2]//tr[position()>2]")[0]
Run Code Online (Sandbox Code Playgroud)
任何人都知道一个奇特的解决方案,将第3行的所有数据都转换为tbl并将其复制到一个数组中,以便将其处理成一个没有lxml依赖的模块?
先谢谢你的帮助,Alex
我正在写一些异步库,并决定支持asyncio和trio并发库来运行它。无论选择哪个库,我都有一些代码试图变得聪明并做正确的事情。
如何检测其中哪一个用于运行我的代码?两者都可以吗?
我创建了一个实用程序函数来从生成器表达式返回预期的单个项目
print one(name for name in ('bob','fred') if name=='bob')
Run Code Online (Sandbox Code Playgroud)
这是一个很好的方法吗?
def one(g):
try:
val = g.next()
try:
g.next()
except StopIteration:
return val
else:
raise Exception('Too many values')
except StopIteration:
raise Exception('No values')
Run Code Online (Sandbox Code Playgroud) 我尝试编写将以递归方式添加到列表中的程序
def string(times,char):
list=[]
list.append(char)
if times==0:
print(list)
else:
return [list] + string(times-1 ,char)
string(3,input('text'))
Run Code Online (Sandbox Code Playgroud)
当我启动代码时,出现错误
TypeError:只能串联列表(不是“ NoneType”)到列表
我目前在使用UDP和Python套接字模块时遇到问题.我们有服务器和客户端.当我们向用户发送数据时会出现问题.用户可能通过客户端崩溃,ISP断开连接或其他一些不正确的方法关闭了与服务器的连接.因此,可以将数据发送到封闭的套接字.
当然,使用UDP,您无法判断数据是否真正到达或是否已关闭,因为它不关心(至少,它不会引发异常).但是,如果您发送数据并且它已关闭,则会以某种方式返回数据(???),最终会在sock.recvfrom上出现套接字错误.[Errno 10054]远程主机强行关闭现有连接.几乎看起来像连接的自动响应.
虽然这很好,但可以通过try:except:block来处理(即使它会降低服务器的性能).问题是,我不知道这是来自谁或什么套接字关闭.有没有找到'谁'(ip,socket#)发送这个?它会很棒,因为我可以立即断开它们并将它们从数据中删除.有什么建议?谢谢.
服务器:
import socket
class Server(object):
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.connected = {}
def connect(self):
self.socket.bind(('127.0.0.1', 5579))
def find_data(self):
while 1:
data, address = self.socket.recvfrom(1024)
self.got_data(data,address)
if self.connected.has_key(address):
pass
else:
self.connected[address] = None
def got_data(self, data, address):
print "GOT",data,"FROM",address
for people in self.connected:
print people
self.send_data('hi', people)
def send_data(self, data, address):
self.socket.sendto(data,address)
if __name__ == '__main__':
server = Server()
server.connect()
print "NOW SEARCHING FOR DATA"
server.find_data()
Run Code Online (Sandbox Code Playgroud)
客户:
import socket, time
class Client(object): …Run Code Online (Sandbox Code Playgroud) 我有一个家庭作业,真的是我的面条.它涉及电梯模拟,该模拟采用用户输入的楼层数和使用电梯的人数.人们的起始楼层和目的地楼层是楼层内的随机数字.
我意识到我的代码非常稀疏,并且存在相当多的空白,但我真的不知道从哪里开始.
我需要在构建类中提供帮助,例如如何使run()和output()部分工作.任何其他提示将非常感谢和有帮助.请注意,我不是在寻找有人为我做代码,而是要握住我的手并告诉我要走哪条路.对我来说,课程似乎完全不可思议.
import random
floors=raw_input('Please enter the number of floors for the simulation:')
while floors.isalpha() or floors.isspace() or int(floors) <=0:
floors=raw_input('Please re enter a digit for number of floors:')
customers=raw_input('Please enter the number of customers in the building:')
while customers.isalpha() or customers.isspace() or int(customers) <0:
customers=raw_input('Please re enter a digit for number of customers:')
count = 1
class building:
def num_of_floors():
num_of_floors = floors
def customer_list():
customer_list = customers
def run(self):
def output(self):
print elevator.cur_floor
class elevator:
def num_of_floors(): …Run Code Online (Sandbox Code Playgroud) 只是关于python和pygame事件处理的noob问题.
我在pygame教程中得到以下代码:
while 1:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
Run Code Online (Sandbox Code Playgroud)
...但由于某种原因,它会返回此错误:
if event.type in (QUIT, KEYDOWN):
NameError: name 'QUIT' is not defined
Run Code Online (Sandbox Code Playgroud)
有谁能解释一下?