Я определил сокет под названием sock в моем файле Main.py. Из Main.py я импортирую Functions.py, где есть функция (или метод, не знаю, как они называются в Python) с именем sendMessage. В sendMessage мне нужно использовать носок, который я определил в своем Main.py. Как мне это сделать? Я попытался добавить global sock
к моей функции / методу, но безрезультатно.
Main.py
#! /usr/bin/env python
import sys
import socket
import string
import os
import commands
import time
from config import *
from functies import *
from php import *
sock = socket.socket ()
sock.connect ((config['server']['host'], config['server']['poort']))
...
Functions.py
#! /usr/bin/env python
def sendMessage (receiver, message):
global sock
sock.send ('PRIVMSG ' + ontvanger + ' :' + message + '\n')
Ошибка
Traceback (most recent call last):
File "Main.py", line 68, in <module>
sendMessage (receiver, config['nick'] + ' is here!')
File "/home/robin/microPy/Functions.py", line 4, in sendMessage
sock.send ('PRIVMSG ' + receiver + ' :' + message + '\n')
NameError: global name 'sock' is not defined