listening socket

Benjamin Kaplan benjamin.kaplan at case.edu
Wed Sep 8 19:06:44 CEST 2010


On Wed, Sep 8, 2010 at 12:59 PM, cerr <ron.eggler at gmail.com> wrote:
> Hi,
>
> I'm trying to create a listening socket connection on port 1514.
> I tried to follow the documentation at:
> http://docs.python.org/release/2.5.2/lib/socket-example.html
> and came up with following lines:
> import socket
>
> host = ''                 # Symbolic name meaning all available
> interfaces
> port = 1514               # Arbitrary non-privileged port
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((host, port))
> s.listen(1)
> conn, addr = s.accept()
> print 'Connected by', addr
> while 1:
>    data = conn.recv(1024)
>    if not data: break
>    conn.send(data)
> conn.close()
> but that is not working, i'm getting this:
> import: unable to open X server `' @ error/import.c/ImportImageCommand/
> 362.
> ./sockettest.py: line 4: host: command not found
> ./sockettest.py: line 5: port: command not found
> ./sockettest.py: line 6: syntax error near unexpected token `('
> ./sockettest.py: line 6: `s = socket.socket(socket.AF_INET,
> socket.SOCK_STREAM)'
>
> now why would it try to open an x server??? :o
> --

Because it's not executing it as a Python program. It's trying to
execute it as a shell script. If you want to run a script as a Python
program, either call the interpreter directly
python sockettest.py

or include a Shebang line as the first line of the file that tells the
computer what interpreter to use
#!/usr/bin/env python

The file extension itself is meaningless to a Unix shell- it's just a
part of the file name.



More information about the Python-list mailing list