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/