我创建了一个python 3.4文件并尝试将一些变量导入另一个python脚本。这两个脚本都在同一个文件夹中,但我一直收到错误。
我可以使用导入,它工作正常。但是,当我尝试使用ServerConfiguration导入端口从脚本导入变量时,ip我收到一条错误,指出NameError:name' ServerConfiguration'未定义
ServerConfiguration模块:
import socket
import sys
import os
serverIP = None
def serverSocket():
PORT = 8884 # Port the server is listening on
ServerEst = input('Has a server been established')
if ServerEst == 'yes':
ServerIP = input ('Enter the servers IP address')
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established at ' + serverIP)
else:
CreateServer = input('Would you like to create a server')
if CreateServer == 'yes':
ServerIP = input('What is you LAN IP? Please remeber if reomte user are allowed to connect port forward port "8884" ')
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established to ' + ServerIP)
else:
print ('Defaulting to default')
ServerIP = 'localhost'
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established to ' + ServerIP)
UserModule:
from ServerConfiguration import serverSocket, serverIP
import socket
import sys
import os
def sendMessage():
sockc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
MESSAGE = input('Type the message to send ')
sockc.sendto((MESSAGE.encode, "utf-8"), (serverIP, PORT))
print(MESSAGE)
ServerConfiguration.serverSocket()
sendMessage()
答案 0 :(得分:2)
如果您使用
from ServerConfiguration import serverSocket, serverIP
你应该写
serverSocket()
没有ServerConfiguration。
另一种方式:
import ServerConfiguration
...
ServerConfiguration.serverSocket()