小编Gen*_*077的帖子

telnetlib python示例

所以我正在尝试python docs给出的这个非常简单的例子:

import getpass
import sys
import telnetlib

HOST = "<HOST_IP>"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
   tn.read_until("Password: ")
   tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
Run Code Online (Sandbox Code Playgroud)

我的问题是它挂在read_all()的末尾...它不打印任何东西.我之前从未使用过这个模块,所以我试图让这个真正基本的例子继续下去.顺便说一下,我正在使用python 2.4谢谢.

python python-2.4 telnetlib

10
推荐指数
3
解决办法
7万
查看次数

类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 _io.TextIOWrapper

我正在尝试使用此处的示例打开、读取、修改和关闭 json 文件:

如何向使用 Python 从文件中检索的 JSON 数据添加键值?

import os
import json

path = '/m/shared/Suyash/testdata/BIDS/sub-165/ses-1a/func'
os.chdir(path)

string_filename = "sub-165_ses-1a_task-cue_run-02_bold.json"

with open ("sub-165_ses-1a_task-cue_run-02_bold.json", "r") as jsonFile:
    json_decoded = json.load(jsonFile)

json_decoded["TaskName"] = "CUEEEE"

with open(jsonFile, 'w') as jsonFIle:
    json.dump(json_decoded,jsonFile) ######## error here that open() won't work with _io.TextIOWrapper
Run Code Online (Sandbox Code Playgroud)

最后我不断收到错误消息(open(jsonFile...)因此我不能使用jsonFile变量 with open()。我使用了上面链接中提供的示例的确切格式,所以我不确定为什么它不起作用。这最终会进入更大的脚本,所以我想远离硬编码/使用字符串作为 json 文件名。

python json file contextmanager

7
推荐指数
1
解决办法
2万
查看次数

python:如何使用api从Google驱动器下载文件

我想使用Google Drive API将Google Drive中的文件下载到我的系统中。我该如何实施呢?

根据https://developers.google.com/drive/v2/reference/files/get#examples中给出的示例

def print_file(service, file_id):

 """Print a file's metadata.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to print metadata for.
  """
  try:
    file = service.files().get(fileId=file_id).execute()

    print 'Title: %s' % file['title']
    print 'MIME type: %s' % file['mimeType']
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def download_file(service, drive_file):
  """Download a file's content.

  Args:
    service: Drive API service instance.
    drive_file: Drive File instance.

  Returns:
    File's content if successful, None otherwise.
  """ …
Run Code Online (Sandbox Code Playgroud)

python-2.7 google-drive-api

5
推荐指数
1
解决办法
1万
查看次数