当前位置: 首页 > 知识库问答 >
问题:

当我运行我的套接字程序时,有get:`import:command not found`

钱志义
2023-03-14

我编写了一个套接字服务器程序员

#-*- coding:utf-8 -*-
# Author:sele

import socket

HOST = '127.0.0.1'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break

            conn.sendall(data)

为什么找不到导入

编辑-01

我把这行插入我的第一行。

sele-MacBook-Pro:test01 ldl$ ./tests02-server.py 
Traceback (most recent call last):
  File "./tests02-server.py", line 11, in <module>
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
AttributeError: __exit__
aircrafts-MacBook-Pro:test01 ldl$ ./tests02-server.py 
Traceback (most recent call last):
  File "./tests02-server.py", line 11, in <module>
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
AttributeError: __exit__

共有1个答案

桂学
2023-03-14

您将程序作为shell脚本运行,而不是python程序。添加适当的#!行:

#!/usr/bin/env python 

或从命令行显式运行:

$ python tests02-server.py
 类似资料: