python-2.7 - ValueError : too many values to unpack when using socket

标签 python-2.7 sockets google-app-engine

import socket
import time
HOST = "192.168.x.x"
PORT = 5454
data = "Hello"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(HOST,PORT)
while  True:
   s.sendto(data(HOST,PORT))
   print "send" + data
   time.sleep(1)
我收到这样的错误:
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~lyfe-playtm/20161117t121204.397114004363270281/main.py", line 24, in <module>
    s.connect(HOST,PORT)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/socket.py", line 222, in meth
    return getattr(self._sock,name)(*args)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 752, in connect
    address_hostname_hint=_hostname_hint)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 590, in _CreateSocket
    address_hostname_hint)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 632, in _SetProtoFromAddr
    address, port = address
ValueError: too many values to unpack
我正在尝试在Google App Engine中使用套接字。这是我在main.py中输入的代码。我如何摆脱这个错误?

最佳答案

17.2. socket — Low-level networking interface:

Socket addresses are represented as follows: ... A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.





socket.connect(address)

Connect to a remote socket at address. (The format of address depends on the address family — see above.)

Note

This method has historically accepted a pair of parameters for AF_INET addresses instead of only a tuple. This was never intentional

and is no longer available in Python 2.0 and later.



这意味着你应该改变
s.connect(HOST,PORT)


s.connect((HOST,PORT))

同样在下面的变化
   s.sendto(data(HOST,PORT))


   s.sendto(data, (HOST,PORT))

旁注:使用s.sendto()时,应在套接字上连接而不是(丢弃s.connect()或使用s.send()s.sendall()):

socket.sendto(string, address) socket.sendto(string, flags, address)

Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address. The optional flags argument has the same meaning as for recv() above. Return the number of bytes sent. (The format of address depends on the address family — see above.)

关于python-2.7 - ValueError : too many values to unpack when using socket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648397/

相关文章:

python-2.7 - 如何运行我的远程服务器 Tryton 3.6.0?

python - 使用 scipy.weave.inline 时出错

Python Awis API 给出未定义的错误

linux - nginx uWSGI 连接到 unix 套接字失败

c - 套接字编程权限被拒绝

python - 如何打印到小数点后两位?

Python套接字.错误: [Errno 113] No route to host but ping works

google-app-engine - Appstats 值是什么意思?

google-app-engine - 如何获取 App Engine 前端服务器的实例 ID?

python - Google App Engine : Reverse Proxy + OpenID, 用户在登录后被重定向到 appspot 域