Reputation: 327
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)
I am gettting an error like this:
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
I am trying to use socket in Google App Engine. This is the code i put in my main.py
. How do i get rid of this error?
Upvotes: 1
Views: 2460
Reputation: 39824
From 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.
and
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.
Which means you should change
s.connect(HOST,PORT)
with
s.connect((HOST,PORT))
Similarly below change
s.sendto(data(HOST,PORT))
with
s.sendto(data, (HOST,PORT))
Side note: when using s.sendto()
the socket should not be connected (either drop the s.connect()
or use s.send()
or 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.)
Upvotes: 5