python 3.2中奇怪的套接字问题
我目前正在用 python 制作一个聊天应用程序。 它的工作原理如下: 1.服务器正常运行 2. 客户输入ID和密码 3.服务器检查登录信息 4. 服务器为它们分配一个随机数 5.他们现在可以从服务器发送和接收消息
我在接收ID和通行证时遇到问题(请不要责怪我使用global和exec)
def recv_server():
global number
global s #the tcp socket
exec("global sock"+number) #creating a specific sock for each connection
exec("sock"+number+","+"sockname"+number+" = s.accept()")
exec("logindata=sock"+number+".recv(65535)")
logindata=logindata.decode()
...
数字为1代码将是:
global sock1
sock1, sockname1 = s.accept()
logindata=sock1.recv(65535)
I'我在 Windows 上使用 python 3.2 并在运行脚本时,当登录数据通常有一个值时,现在它未分配
File 'abc' line 23, in recv_server
logindata=logindata.decode()
UnboundLocalError: local variable 'logindata' referenced before assigment
在我的 Linux 计算机(mint 11)上,与 dropbox 共享文件并使用 python(2.7)打开不会给我任何错误
现在我问是否有人知道这个问题的原因是什么?
I'm currently making a chat application in python.
It works like this:
1. The server is working
2. Clients enter their id and password
3. The login info are checked by the server
4. They are assigned a random number by the server
5. They can now send and recv message from and to the server
I'm having a problem at receiving the ID and the pass (please don't blame me for using global and exec)
def recv_server():
global number
global s #the tcp socket
exec("global sock"+number) #creating a specific sock for each connection
exec("sock"+number+","+"sockname"+number+" = s.accept()")
exec("logindata=sock"+number+".recv(65535)")
logindata=logindata.decode()
...
Number being 1 the code would be:
global sock1
sock1, sockname1 = s.accept()
logindata=sock1.recv(65535)
I'm using python 3.2 on windows and when running the script, when login data would normaly have a value, now it's unassigned
File 'abc' line 23, in recv_server
logindata=logindata.decode()
UnboundLocalError: local variable 'logindata' referenced before assigment
On my Linux computer(mint 11), sharing the file with dropbox and opening with python (2.7) doesn't give me any error
Now I'm asking if anybody knows what seems to be the cause of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哎哟哎哟哎哟。
关于
global
的内容,我让你来纠正,但是定义了
sockets
后,也许sockets = {}
左右就可以了至少尝试明智地编码。然后
exec
消失了,只剩下global
,恕我直言,这不太好,但也不像另一个那么难看。您的错误源于编译器无法识别由于
exec
内容而发生的logindata
的第一次分配。Ouch ouch ouch.
About the
global
stuff, I let it up to you to correct, butwith a
sockets
defined maybesockets = {}
or so would be at least a try to code sensibly.Then the
exec
is gone and only theglobal
stays which is IMHO not good, but not as ugly as the other.Your error stems from the compiler not recognizing the first assignment of
logindata
to happen because of theexec
stuff.尝试根本不使用 exec。
我认为工作中对全球员工和本地员工存在很大的误解
这里。首先,如果
number
和s
没有分配给本地(函数)作用域,则无需将它们声明为
全局
,因为它们只能被读取。其次,为什么是sockname#需要是本地范围内的 sockname#。它可能只是 sockname
也许您来自一种没有与 Python 相同范围的语言?如果是这样,如果您花时间熟悉 Python 中本地作用域和全局作用域之间的区别,您的生活会容易得多。
Try not using exec at all.
I think there's a big misunderstanding of globals and locals at work
here. First of all, if
number
ands
are not assigned to in thelocal (function) scope, then there's no need to declare them as
global
s as they will only be read. Secondly, why doess sockname#need to be sockname# in the local scope. It could just be sockname
Perhaps you're coming from a language w/o the same kind of scoping as Python? If so your life would be much easier if you took the time to become familiar with the difference between local and global scope in Python.