python 3.2中奇怪的套接字问题

发布于 2024-11-30 07:04:56 字数 930 浏览 1 评论 0原文

我目前正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫漫岁月 2024-12-07 07:04:56

哎哟哎哟哎哟。

关于 global 的内容,我让你来纠正,但是

def recv_server():
    global number
    global s     #the tcp socket
    global sockets
    sockets[number], sockname = s.accept()
    logindata = sockets[number].recv(65535)
    logindata = logindata.decode()
    ...

定义了 sockets 后,也许 sockets = {} 左右就可以了至少尝试明智地编码。

然后 exec 消失了,只剩下 global ,恕我直言,这不太好,但也不像另一个那么难看。

您的错误源于编译器无法识别由于 exec 内容而发生的 logindata 的第一次分配。

Ouch ouch ouch.

About the global stuff, I let it up to you to correct, but

def recv_server():
    global number
    global s     #the tcp socket
    global sockets
    sockets[number], sockname = s.accept()
    logindata = sockets[number].recv(65535)
    logindata = logindata.decode()
    ...

with a sockets defined maybe sockets = {} or so would be at least a try to code sensibly.

Then the exec is gone and only the global 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 the exec stuff.

似狗非友 2024-12-07 07:04:56

尝试根本不使用 exec。

def recv_server():
    globals()["sock"+number], locals()["sockname"+number] = s.accept()
    logindata = locals()["sock"+number].recv(65535)
    logindata=logindata.decode()

我认为工作中对全球员工和本地员工存在很大的误解
这里。首先,如果 numbers 没有分配给
本地(函数)作用域,则无需将它们声明为
全局,因为它们只能被读取。其次,为什么是sockname#
需要是本地范围内的 sockname#。它可能只是 sockname

也许您来自一种没有与 Python 相同范围的语言?如果是这样,如果您花时间熟悉 Python 中本地作用域和全局作用域之间的区别,您的生活会容易得多。

Try not using exec at all.

def recv_server():
    globals()["sock"+number], locals()["sockname"+number] = s.accept()
    logindata = locals()["sock"+number].recv(65535)
    logindata=logindata.decode()

I think there's a big misunderstanding of globals and locals at work
here. First of all, if number and s are not assigned to in the
local (function) scope, then there's no need to declare them as
globals 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文