ValueError:使用套接字

时间:2016-11-17 06:49:19

标签: 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中输入的代码。我如何摆脱这个错误?

1 个答案:

答案 0 :(得分:3)

来自17.2. socket — Low-level networking interface

  

套接字地址表示如下:...一对(主机,端口)   用于AF_INET地址族,其中host是一个字符串   表示Internet域表示法中的主机名   &#39; daring.cwi.nl&#39;或者像&#39; 100.50.200.5&#39;这样的IPv4地址,端口是   整数。

  

插座。连接(地址)

     

连接到地址处的远程套接字。 (地址的格式取决于   关于地址族 - 见上文。)

Note

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

并且在Python 2.0及更高版本中不再可用。

这意味着你应该改变

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)

     

将数据发送到套接字。套接字不应连接到a   远程套接字,因为目标套接字是由地址指定的。   可选的flags参数与上面的recv()具有相同的含义。   返回发送的字节数。 (地址的格式取决于   地址系列 - 见上文。)